Repeat Until 0 (Use Do While) - 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 repeating the process until the user enters 0. To achieve this, a do-while loop will be used, which ensures that the operation is performed at least once before checking the condition. The do-while loop is ideal when you need the code to run at least once, regardless of the initial condition. This exercise is perfect for enhancing your skills in C# and using loops that control the program’s execution, allowing iterations based on user input.

Completing this exercise will help you understand the execution flow in C# using a do-while loop, a control flow structure that repeats instructions until a specific condition is met. You will also learn how to handle user input effectively.

 Category

Flow Control

 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

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

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

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

  •  One or two negative numbers

    In this C# exercise, you will learn how to write a program that prompts the user to enter two numbers and then determines whether both numbers are negative, only one is nega...