Displaying Odd Numbers from 15 to 7 Using a While Loop in C#

This exercise demonstrates how to use a while loop in C# to display odd numbers in descending order. In this case, the program will start at 15 and display every odd number down to 7.

Using the while loop allows you to control the flow of the program based on a condition that is evaluated before each iteration. By starting at 15 and decrementing by 2 in each iteration, the program prints only odd numbers from 15 to 7. This task provides hands-on practice with loops, conditions, and number manipulation.



Group

C# Flow Control Basics

Objective

The objective of this exercise is to write a C# program to display the odd numbers from 15 to 7 (downwards) on the screen using a 'while' loop.

Write a C# program to display the odd numbers from 15 to 7 (downwards) on the screen using "while".

Example C# Exercise

 Copy C# Code
// First and Last Name: John Doe

using System;

namespace OddNumbersWhileLoop
{
    class Program
    {
        // Main method to start the program execution
        static void Main(string[] args)
        {
            // Declare and initialize a variable to start from 15
            int number = 15;

            // Start the while loop; it will run as long as number is greater than or equal to 7
            while (number >= 7)
            {
                // Check if the number is odd (this is ensured by the starting number being odd)
                if (number % 2 != 0)
                {
                    // Display the current odd number
                    Console.WriteLine(number);
                }

                // Decrease the number by 2 to move to the next odd number downwards
                number -= 2;
            }

            // Wait for user input before closing the console window
            Console.ReadKey(); // Keeps the console open until a key is pressed
        }
    }
}

 Output

15
13
11
9
7

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

  • Sum of User Input Numbers Until Zero is Entered in C#

    This exercise teaches you how to use a loop to ask the user for an undetermined number of inputs and calculate their sum. The program will continue to ask the user for numbers unti...

  • Check if Both Numbers Are Negative in C#

    This exercise helps you practice the use of conditionals in C# to determine whether two input numbers are negative or not. The program asks the user to input two numbers and checks...

  • Check if One or Both Numbers are Negative in C#

    In this exercise, you will create a C# program that prompts the user to enter two numbers and checks the conditions for negativity. The program will determine whether both numbers ...

  • Display Multiples of Both 3 and 5 in C#

    In this exercise, you'll create a C# program that displays all numbers between 1 and 500 that are divisible by both 3 and 5. This task helps practice the use of loops and the modul...

  • Repeat a Number Based on User Input in C#

    In this exercise, you will write a C# program that asks the user for two inputs: a number and a quantity. The program will then repeat the number as many times as specified by the ...

  • Login Verification Program in C#

    In this task, you'll create a simple login verification program using C#. The program will continuously ask the user to enter their login and password until the correct values are ...