C# Program to Check Whether a Given Number is Even or Odd

In this example, we’ll explore a simple C# program to determine if a given number is even or odd, showcasing the language’s ability to handle basic logic.

Problem Statement

Write a C# program to check whether a given integer is even or odd. The program should take an input number and determine if it’s divisible by 2. If it is, the number is even; otherwise, it’s odd.

C# Program to Check Whether a Given Number is Even or Odd

using System;

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

        if (IsEven(number))
        {
            Console.WriteLine(number + " is even.");
        }
        else
        {
            Console.WriteLine(number + " is odd.");
        }
    }

    static bool IsEven(int num)
    {
        return num % 2 == 0;
    }
}

Input / Output

C# Program to Check Whether a Given Number is Even or Odd

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