While + Counter - C# Programming Exercise

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 flow structure that executes a block of code as long as a specific condition is true. In this case, the program must display the numbers sequentially until it reaches 10. This type of exercise is essential to familiarize yourself with using loops in C# and understand how to control the execution of the code efficiently. The while loop is particularly useful when you don’t know how many times a task should be repeated but rather when a condition is met on each iteration. Completing this exercise will improve your understanding of C# loops and their practical application in simple programming tasks.

This exercise will help you strengthen your skills with loops in C#, specifically the while loop, to perform repetitive tasks in a straightforward and efficient manner.

 Category

Flow Control

 Exercise

While + Counter

 Objective

Write a C# program to display the numbers 1 to 10 on the screen 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 counter = 1; // Initializing the counter variable to 1

        // Using a while loop to display numbers from 1 to 10
        while (counter <= 10) // The loop will continue as long as counter is less than or equal to 10
        {
            // Displaying the current value of counter
            Console.WriteLine(counter); // Printing the current number

            counter++; // Incrementing the counter by 1 after each iteration
        }
    }
}

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

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

  •  Multiples

    In this exercise, you will learn how to use the modulo operator % to find numbers between 1 and 500 that are divisible by both 3 and 5. The program will...