The Program in C# Program to Print Odd numbers between Given Range are given below:
using System;
namespace PrintOddNumbers
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello Codeauri Family, Enter the Lower and Upper Limit of range here to find odd numbers between them:");
Console.WriteLine("Well,enter the lower limit of the range: ");
int lowerLimit = int.Parse(Console.ReadLine());
Console.WriteLine("Well,enter the upper limit of the range: ");
int upperLimit = int.Parse(Console.ReadLine());
Console.WriteLine("Okay, the odd numbers between " + lowerLimit + " and " + upperLimit + " are: ");
for (int i = lowerLimit; i <= upperLimit; i++)
{
if (i % 2 != 0)
{
Console.WriteLine(i);
}
}
}
}
}
Output:
[?2004l
Hello Codeauri Family, Enter the Lower and Upper Limit of range here to find odd numbers between them:
Well,enter the lower limit of the range:1
Well,enter the upper limit of the range:10
Okay, the odd numbers between 1 and 10 are:
1
3
5
7
9
Pro-Tips💡
Here are step by step execution of above program:
- The first line “using System;” is a directive that brings the System namespace into scope and makes its classes and methods available to the code.
- The namespace declaration “namespace PrintOddNumbers” creates a new namespace with the name “PrintOddNumbers”.
- The class declaration “class Program” creates a new class with the name “Program”.
- The “Main” method is the entry point of the application. It prints a message to the console asking the user to enter the lower and upper limits of the range.
- The code reads the lower limit of the range from the console using Console.ReadLine() and converts it to an integer using int.Parse().
- The code reads the upper limit of the range from the console using Console.ReadLine() and converts it to an integer using int.Parse().
- The code uses a for loop to iterate through the range defined by the lower and upper limits. The loop variable “i” is initialized to the lower limit and increments by 1 in each iteration until it reaches the upper limit.
- In each iteration, the code checks if the current value of “i” is odd by using the expression
i % 2 != 0
. If it is odd, the code writes the value of “i” to the console using Console.WriteLine(). - After the for loop finishes, the program ends.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.