Objective
Write a C# program to ask the user for a number "x" and display 10*x. It must repeat until the user enters 0 (using "while").
Example C# Exercise
Show C# Code
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
// 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
// Using a while loop to repeat the process until the user enters 0
while (x != 0) // While the user enters a number different from 0
{
// Displaying the result of 10 times the entered number
Console.WriteLine("10 * {0} = {1}", x, 10 * x); // Printing the result of 10 * x
// Asking the user to enter another number
Console.Write("Enter a number (enter 0 to stop): ");
x = Convert.ToInt32(Console.ReadLine()); // Converting the input to an integer again
}
// Displaying a message when the loop ends (when 0 is entered)
Console.WriteLine("You entered 0. The program has stopped."); // Printing a stop message
}
}
Output
Case 1:
Enter a number (enter 0 to stop): 5
10 * 5 = 50
Enter a number (enter 0 to stop): 3
10 * 3 = 30
Enter a number (enter 0 to stop): 0
You entered 0. The program has stopped.
Case 2:
Enter a number (enter 0 to stop): 7
10 * 7 = 70
Enter a number (enter 0 to stop): 10
10 * 10 = 100
Enter a number (enter 0 to stop): 0
You entered 0. The program has stopped.
Case 3:
Enter a number (enter 0 to stop): -4
10 * -4 = -40
Enter a number (enter 0 to stop): 2
10 * 2 = 20
Enter a number (enter 0 to stop): 0
You entered 0. The program has stopped.
Case 4:
Enter a number (enter 0 to stop): 0
You entered 0. The program has stopped.
More C# Programming Exercises of Flow Control
Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.
In this C# exercise, you will learn how to write a program that asks the user to enter a number "x" and displays the result of multiplying it by 10. The program will keep re...
In this C# exercise, you will learn how to write a program that displays the numbers from 1 to 10 on the screen using a while loop. The while loop is a control...
In this C# exercise, you will learn how to write a program that asks the user to enter a number and then displays its multiplication table using a while loop. The ...
In this C# exercise, you will learn how to write a program that displays the odd numbers from 15 down to 7 using a while loop. The while loop is a control stru...
In this C# exercise, you will learn how to write a program that asks the user to enter an undetermined amount of numbers (until 0 is entered) and displays the total sum of t...
In this C# exercise, you will learn how to write a program that prompts the user to enter two numbers and determines whether both numbers are negative or not. This type of p...