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
Show 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