Group
C# Flow Control Basics
Objective
The objective of this exercise is to write a C# program that asks the user for a number "x" and displays 10 * x. The program must repeat the process until the user enters 0, using "do-while".
Write a C# program that asks the user for a number "x" and displays 10 * x. The program must repeat the process until the user enters 0, using "do-while".
Example C# Exercise
Show C# Code
using System;
namespace MultiplyByTenDoWhile
{
class Program
{
static void Main(string[] args)
{
int x;
do
{
Console.Write("Enter a number (0 to stop): ");
x = Convert.ToInt32(Console.ReadLine());
if (x != 0)
{
Console.WriteLine("10 * " + x + " = " + (10 * x));
}
} while (x != 0);
Console.WriteLine("You entered 0. Program is exiting.");
Console.ReadKey();
}
}
}
Output
//Example 1 (user enters several numbers):
Enter a number (0 to stop): 5
10 * 5 = 50
Enter a number (0 to stop): 7
10 * 7 = 70
Enter a number (0 to stop): 12
10 * 12 = 120
Enter a number (0 to stop): 0
You entered 0. Program is exiting.
//Example 2 (user enters zero first):
Enter a number (0 to stop): 0
You entered 0. Program is exiting.