Repetitive Structures - C# Programming Exercise

This C# exercise aims to develop a program that prompts the user for two numbers and displays all the numbers between them (inclusive) three times using for, while, and do while loops. The program will first ask for the first number and the last number, then print the numbers between them, including both the starting and ending numbers. This exercise is helpful for learning how to use different types of loops in C# and how to control the flow of the program while iterating over a range of numbers. Using the for, while, and do while loops allows you to compare their characteristics and choose the most appropriate one for each situation. It is also a great opportunity to practice handling user input and working with integer values.

 Category

Flow Control

 Exercise

Repetitive Structures

 Objective

Write a C# program that prompts the user for two numbers and displays the numbers between them (inclusive) three times using "for", "while", and "do while" loops.

Enter the first number: 6
Enter the last number: 12

6 7 8 9 10 11 12
6 7 8 9 10 11 12
6 7 8 9 10 11 12

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

        // Prompt the user to enter the last number
        Console.Write("Enter the last number: ");
        int lastNumber = int.Parse(Console.ReadLine());  // Reading the last number entered by the user

        // Using a for loop to display numbers from firstNumber to lastNumber
        Console.WriteLine("Using for loop:");
        for (int i = firstNumber; i <= lastNumber; i++)  // Loop runs from firstNumber to lastNumber
        {
            Console.Write(i + " ");  // Display the current number
        }
        Console.WriteLine();  // Move to the next line after printing the numbers

        // Using a while loop to display numbers from firstNumber to lastNumber
        Console.WriteLine("Using while loop:");
        int j = firstNumber;  // Initialize the counter for the while loop
        while (j <= lastNumber)  // Loop runs while j is less than or equal to lastNumber
        {
            Console.Write(j + " ");  // Display the current number
            j++;  // Increment the counter
        }
        Console.WriteLine();  // Move to the next line after printing the numbers

        // Using a do-while loop to display numbers from firstNumber to lastNumber
        Console.WriteLine("Using do-while loop:");
        int k = firstNumber;  // Initialize the counter for the do-while loop
        do
        {
            Console.Write(k + " ");  // Display the current number
            k++;  // Increment the counter
        }
        while (k <= lastNumber);  // Loop continues while k is less than or equal to lastNumber
        Console.WriteLine();  // Move to the next line after printing the numbers
    }
}

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

  •  Digits in a number

    This C# exercise aims to teach how to develop a program that calculates the number of digits in a positive integer. The program uses an efficient technique by perform...

  •  Hollow square

    This C# exercise aims to develop a program that asks the user for a symbol and a width, and then displays a hollow square of that width using the provid...

  •  Product

    This C# exercise aims to develop a program that asks the user for two integer numbers and shows their multiplication, but without using the "*" operator. Inste...

  •  Absolute value

    This C# exercise aims to develop a program that calculates (and displays) the absolute value of a number x. The absolute value of a number is its distan...

  •  Hollow rectangle

    This C# exercise aims to develop a program that prompts the user for a symbol, a width, and a height, then displays a hollow rectangle of the spe...

  •  Statistics

    This C# exercise aims to develop a program that allows the user to input several numbers and calculate basic statistical operations such as sum, average, mi...