Repeat Until 0 - C# Programming Exercise

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 asking for new numbers and displaying the multiplication result until the user enters 0. To achieve this, a while loop will be used to repeatedly perform the operation until a specific condition is met, in this case, when the user enters 0. This exercise is perfect for practicing the use of loops in C# and improving your ability to work with user input and repetition structures. You will also learn how to control the program flow with conditions that stop the loop when a certain criterion is met.

Completing this exercise will help you understand how while loops work in C# and how to control the execution flow based on user interactions.

 Category

Flow Control

 Exercise

Repeat Until 0

 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").

 Write Your C# Exercise

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
    }
}

 Share this C# exercise

 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#.

  •  Repeat until 0 (Use Do While)

    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...

  •  While + Counter

    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...

  •  Multiplication table (use while)

    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 ...

  •  Odd numbers descending

    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...

  •  Sum numbers

    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...

  •  Two negative numbers

    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...