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

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

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...