C# Program to Print Odd Numbers in a Given Range

In this example, we’ll create a C# program that prints odd numbers within a specified range, showcasing the language’s capabilities in implementing loops and conditional statements.

Problem Statement

Write a C# program to print all the odd numbers within a given range. The program should take two integer inputs, representing the start and end of the range, and then display all the odd numbers within that range.

C# Program to Print Odd Numbers in a Given Range

using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter the starting number: ");
        int start = Convert.ToInt32(Console.ReadLine());

        Console.Write("Enter the ending number: ");
        int end = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Odd numbers in the range from {0} to {1}:", start, end);

        PrintOddNumbersInRange(start, end);
    }

    static void PrintOddNumbersInRange(int start, int end)
    {
        for (int i = start; i <= end; i++)
        {
            if (i % 2 != 0)
            {
                Console.WriteLine(i);
            }
        }
    }
}

Input / Output

C# Program to Print Odd Numbers in a Given Range

Share:

Leave A Reply

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

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...