Number Repeated - C# Programming Exercise

This exercise teaches you how to display a number repeated a number of times specified by the user. Three loop structures will be used to perform the repetition: while, do-while, and for. The user will enter a number and a quantity, and the program will display the number repeated as many times as the specified quantity. This is a great way to practice using loops in C#, a fundamental concept in programming.

 Category

Flow Control

 Exercise

Number Repeated

 Objective

Write a C# program that asks the user for a number and a quantity, and displays that number repeated as many times as the user has specified. Here's an example:

Enter a number: 4
Enter a quantity: 5

44444

You must display it three times: first using "while", then "do-while" and finally "for".

 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()
    {
        // Asking the user to enter a number
        Console.Write("Enter a number: ");
        int number = int.Parse(Console.ReadLine()); // Reading the number entered by the user

        // Asking the user to enter the quantity of repetitions
        Console.Write("Enter a quantity: ");
        int quantity = int.Parse(Console.ReadLine()); // Reading the quantity entered by the user

        // First: Using a "while" loop to display the number the specified quantity of times
        Console.WriteLine("Using while loop:");
        int count = 0; // Initializing a counter variable
        while (count < quantity) // Loop continues as long as count is less than the quantity
        {
            Console.Write(number); // Displaying the number
            count++; // Incrementing the counter
        }
        Console.WriteLine(); // Moving to a new line after the repetition

        // Second: Using a "do-while" loop to display the number the specified quantity of times
        Console.WriteLine("Using do-while loop:");
        count = 0; // Re-initializing the counter variable
        do
        {
            Console.Write(number); // Displaying the number
            count++; // Incrementing the counter
        } while (count < quantity); // Loop continues as long as count is less than the quantity
        Console.WriteLine(); // Moving to a new line after the repetition

        // Third: Using a "for" loop to display the number the specified quantity of times
        Console.WriteLine("Using for loop:");
        for (int i = 0; i < quantity; i++) // Looping from 0 to quantity-1
        {
            Console.Write(number); // Displaying the number
        }
        Console.WriteLine(); // Moving to a new line after the repetition
    }
}

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

  •  Password

    In this C# programming exercise, the user is asked to enter their login and password, both as integer numbers. The goal is to create a program that repeatedly ...

  •  Password V2

    This C# exercise aims to create a program that asks the user for their login and password (both must be integer numbers) and repeats the request until the ente...

  •  Many divisions

    This C# exercise aims to develop a program that asks the user for two numbers and displays the result of the division of these numbers along with the remainder of the divisi...

  •  Several multiplication tables, (use do while)

    This C# exercise aims to create a program that displays multiplication tables from 2 to 6 using nested do...while loops. A do...while loop executes a block of ...

  •  Square

    This C# exercise aims to create a program that prompts the user to enter a number and a width, then displays a square with the given width, using...

  •  Break & continue

    This C# exercise aims to develop a program that displays the even numbers from 10 to 20, both inclusive, except for 16, using three different approaches...