Multiplying a Number Using a Do-While Loop in C#

This exercise demonstrates how to use a do-while loop in C# to repeat a process at least once and continue until a certain condition is met. The program prompts the user to enter a number "x" and calculates 10 * x. The process will repeat until the user enters 0. The do-while loop is useful when you want to ensure that the code block is executed at least once, regardless of the condition, and then keep repeating as long as the condition is true.

This example will help you understand how the do-while loop works, how to capture user input, and how to perform simple mathematical operations in C#.



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

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

Share this C# Exercise

More C# Practice Exercises of C# Flow Control Basics

Explore our set of C# Practice 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#.