Exercise
Repeat Until 0 (Use Do While)
Objective
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 Your C# Exercise
C# Exercise Example
using System; // Importing the System namespace to use Console functionalities
class Program
{
// Main method where the program execution begins
static void Main()
{
int x; // Declaring a variable to store the number entered by the user
// Using a do-while loop to repeat the process at least once
do
{
// Asking the user to enter a number
Console.Write("Enter a number (enter 0 to stop): ");
x = Convert.ToInt32(Console.ReadLine()); // Converting the input to an integer
// Checking if the entered number is not 0
if (x != 0)
{
// Displaying the result of 10 times the entered number
Console.WriteLine("10 * {0} = {1}", x, 10 * x); // Printing the result of 10 * x
}
} while (x != 0); // Repeating the process until the user enters 0
// Displaying a message when the loop ends (when 0 is entered)
Console.WriteLine("You entered 0. The program has stopped."); // Printing a stop message
}
}