Odd Numbers Descending - C# Programming Exercise

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 structure that allows you to execute a block of code repeatedly while a specific condition is true. In this case, the loop will continue running while the number is greater than or equal to 7, and in each iteration, the number will decrease by 2, showing only the odd numbers in this range. This exercise is perfect for improving your skills in using loops in C# and understanding how to control the flow of the program to efficiently work with number sequences.

Using the while loop in this program will help you understand how to perform repetitive tasks, such as displaying a series of numbers in descending order, without having to write multiple lines of code. This exercise is also useful for learning how to work with conditions inside loops in C#, which is essential for handling different programming scenarios.

 Category

Flow Control

 Exercise

Odd Numbers Descending

 Objective

Write a C# program to display the odd numbers from 15 to 7 (downwards) 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 number = 15; // Declaring and initializing the starting number as 15

        // Using a while loop to display odd numbers from 15 to 7 in descending order
        while (number >= 7) // The loop continues as long as the number is greater than or equal to 7
        {
            // Checking if the current number is odd
            if (number % 2 != 0) // If the number is not divisible by 2, it is odd
            {
                // Printing the current odd number
                Console.WriteLine(number); // Displaying the current odd number
            }

            number--; // Decrementing the number 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#.

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

  •  Number repeated

    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, d...

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