0
0
CsharpProgramBeginner · 2 min read

C# Program to Check Even or Odd Number

In C#, you can check if a number is even or odd by using the modulus operator % like this: if (number % 2 == 0) { /* even */ } else { /* odd */ }.
📋

Examples

Input4
Output4 is even
Input7
Output7 is odd
Input0
Output0 is even
🧠

How to Think About It

To check if a number is even or odd, divide it by 2 and look at the remainder. If the remainder is 0, the number is even because it divides evenly by 2. If the remainder is 1, the number is odd because it leaves a leftover when divided by 2.
📐

Algorithm

1
Get the input number from the user.
2
Calculate the remainder when the number is divided by 2 using the modulus operator.
3
If the remainder is 0, the number is even.
4
Otherwise, the number is odd.
5
Print the result.
💻

Code

csharp
using System;

class Program {
    static void Main() {
        Console.Write("Enter a number: ");
        int number = int.Parse(Console.ReadLine() ?? "0");
        if (number % 2 == 0) {
            Console.WriteLine($"{number} is even");
        } else {
            Console.WriteLine($"{number} is odd");
        }
    }
}
Output
Enter a number: 5 5 is odd
🔍

Dry Run

Let's trace the input 5 through the code

1

Input number

User enters 5, so number = 5

2

Calculate remainder

5 % 2 = 1

3

Check remainder

Since remainder is 1, number is odd

4

Print result

Output: "5 is odd"

StepNumberRemainderResult
151Odd
💡

Why This Works

Step 1: Using modulus operator

The % operator gives the remainder of division, which helps us find if a number divides evenly by 2.

Step 2: Even number condition

If the remainder is 0, the number is even because it divides exactly by 2.

Step 3: Odd number condition

If the remainder is not 0, the number is odd because it leaves a leftover when divided by 2.

🔄

Alternative Approaches

Using bitwise AND operator
csharp
using System;

class Program {
    static void Main() {
        Console.Write("Enter a number: ");
        int number = int.Parse(Console.ReadLine() ?? "0");
        if ((number & 1) == 0) {
            Console.WriteLine($"{number} is even");
        } else {
            Console.WriteLine($"{number} is odd");
        }
    }
}
This method uses bitwise AND to check the last bit; it's faster but less intuitive for beginners.
Using a function to return string
csharp
using System;

class Program {
    static string EvenOrOdd(int num) {
        return (num % 2 == 0) ? "even" : "odd";
    }
    static void Main() {
        Console.Write("Enter a number: ");
        int number = int.Parse(Console.ReadLine() ?? "0");
        Console.WriteLine($"{number} is {EvenOrOdd(number)}");
    }
}
This approach separates logic into a function, improving code reuse and clarity.

Complexity: O(1) time, O(1) space

Time Complexity

Checking even or odd uses a single modulus operation, which takes constant time.

Space Complexity

The program uses a fixed amount of memory for variables, so space is constant.

Which Approach is Fastest?

The bitwise AND method is slightly faster but both methods run in constant time and are efficient.

ApproachTimeSpaceBest For
Modulus operator (%)O(1)O(1)Simplicity and readability
Bitwise AND (&)O(1)O(1)Performance in low-level or large-scale code
Function returning stringO(1)O(1)Code reuse and clarity
💡
Use number % 2 == 0 to quickly check if a number is even in C#.
⚠️
Beginners often forget to parse input to an integer before checking even or odd.