Basic Programming C# Examples

C# Program to Print Odd numbers between Given Range

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:

  1. 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.
  2. The namespace declaration “namespace PrintOddNumbers” creates a new namespace with the name “PrintOddNumbers”.
  3. The class declaration “class Program” creates a new class with the name “Program”.
  4. 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.
  5. The code reads the lower limit of the range from the console using Console.ReadLine() and converts it to an integer using int.Parse().
  6. The code reads the upper limit of the range from the console using Console.ReadLine() and converts it to an integer using int.Parse().
  7. 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.
  8. 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().
  9. 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.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X