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
// First and Last Name: John Doe
using System;
namespace MultiplyByTenDoWhile
{
class Program
{
// Main method to start the program execution
static void Main(string[] args)
{
// Declare a variable to store the user's input
int x;
// Initialize the do-while loop
do
{
// Ask the user to input a number
Console.Write("Enter a number (0 to stop): ");
x = Convert.ToInt32(Console.ReadLine()); // Read the user's input and convert it to an integer
// If x is not zero, calculate and display the result of 10 * x
if (x != 0)
{
Console.WriteLine("10 * " + x + " = " + (10 * x));
}
} while (x != 0); // Continue looping until the user enters 0
// Message when the loop ends
Console.WriteLine("You entered 0. Program is exiting.");
// Wait for user input before closing the console window
Console.ReadKey(); // Keeps the console open until a key is pressed
}
}
}
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.