Check Even Number in C#

Beginner
⏱️ 8 min read
📚 Updated: May 2026
🎯 2 Code Examples
Parity

What you’ll learn

  • What “even” means for integers (including zero) and how that maps to code.
  • The usual C# check n % 2 == 0, wrapped in a small IsEven helper that returns true or false.
  • How to reuse the same helper inside a loop to print every even value in a range such as 1 through 10.
  • A browser live preview plus short notes on negatives and optional bitwise shortcuts.

Overview

Even and odd split the integers into two teams. The simplest interview-friendly rule in C# is number % 2 == 0: if the remainder after dividing by 2 is zero, the number is even; otherwise it is odd.

Two small programs

One checks a single value (like 10), the other lists evens from 1 to 10.

Live preview

Try integers such as 0, -4, or 11 and see the remainder line plus the verdict.

Beginner-friendly rigor

We spell out zero, typical signed remainder behavior in C#, and why inclusive loop bounds matter.

Prerequisites

You should already be comfortable with tiny C# programs: a class, Main, printing to the console, and a for loop.

  • C# basics: using System;, class Program, static void Main(), Console.WriteLine.
  • The remainder operator %, if/else, and integer for loops.
  • Optional: methods that return bool (true/false).

What is an even number?

An integer n is even when you can write it as n = 2 × k for some whole number k. On the number line the evens hop by twos: …, -4, -2, 0, 2, 4, ….

In everyday coding, “divisible by 2 with no leftover” is the same idea as “remainder 0 after dividing by 2.” That is exactly what n % 2 == 0 checks.

Even n % 2 == 0
Odd n % 2 != 0
Zero counts as even

Remainder after dividing by 2

Even integers are exactly those that leave remainder 0 when you divide by 2. Odd integers leave a nonzero remainder. In school notation people sometimes write “n is a multiple of 2”—same thing.

Quick example: 10

10 ÷ 2 = 5 with remainder 0, so 10 is even. In C# you would see 10 % 2 == 0 evaluate to true.

Intuition

8 Even
Picture it
four pairs of items with none left over
Code check
8 % 2 == 0
7 Odd
Remainder
7 % 2 is 1 in C#
Verdict
not even

Takeaway: parity is one of the simplest “filters” on integers, and it shows up everywhere from loops to games.

Live preview

This mini tool runs in your browser (no server). It uses JavaScript’s remainder with the same idea as the C# samples: remainder 0 means even. Stick to integers that fit JavaScript’s safe integer range.

  1. Type an integer (try 10, 0, 11, or -4).
  2. Press Check parity or hit Enter.
  3. Read the remainder line and the plain-language sentence.

Whole numbers only; very large values are rejected so the tab stays fast.

Live result
Press “Check parity” to classify.

Algorithm

Goal: decide whether a given integer n is even.

Compute n % 2

If the remainder equals 0, n is even; otherwise it is odd. In C# this is clearest as return n % 2 == 0; inside a bool helper.

Optional: scan a range

Choose start and end. For each i from start through end (inclusive), call the same test and print i when it passes.

📜 Pseudocode

Pseudocode
function isEven(n):
    return (n mod 2) = 0

function printEvensInRange(start, end):
    for i from start to end:
        if isEven(i):
            output i
1

Check a single number

IsEven returns true or false. Main picks a sample value (10) and prints one friendly line—swap that literal or read from the console for your own tests.

c#
using System;

class Program
{
    static bool IsEven(int number)
    {
        return number % 2 == 0;
    }

    static void Main()
    {
        int number = 10;

        if (IsEven(number))
            Console.WriteLine($"{number} is an even number.");
        else
            Console.WriteLine($"{number} is not an even number.");
    }
}

Explanation

Three ideas carry the whole example: a tiny predicate, a branch in Main, and string interpolation for readable output.

return number % 2 == 0;

The rule. % is remainder; comparing to 0 yields a bool that is true exactly for even integers.

if (IsEven(number))

Use the helper. Keeping the math inside IsEven makes Main read like English and keeps the range program easy to reuse.

Console.WriteLine($"{number} is an even number.");

Interpolation. The $"..." form drops the current value of number straight into the sentence.

2

Even numbers between 1 and 10

Same pattern as the reference walkthrough: loop across an inclusive range and print values where IsEven(i) is true. Changing start and end swaps the interval instantly.

c#
using System;

class Program
{
    static void Main()
    {
        int start = 1;
        int end = 10;

        Console.WriteLine("Even numbers in the range 1 to 10:");

        for (int i = start; i <= end; i++)
        {
            if (IsEven(i))
                Console.Write(i + " ");
        }

        Console.WriteLine();
    }

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

Explanation

The outer for visits every integer in the closed interval; the inner if keeps only multiples of two. Trailing Console.WriteLine() finishes the line after the spaced prints.

for (int i = start; i <= end; i++)

Inclusive bounds. <= end matters when the problem says “through 10”—otherwise you would accidentally skip the last value.

Console.Write(i + " ");

Inline spacing. Each accepted i prints with a trailing space; WriteLine after the loop adds the final newline.

Optimization and variations

Step by two. If you only need even outputs, you can jump i += 2 after aligning start to the next even—fewer iterations than testing every integer.

Bitwise parity. On typical two’s complement ints, (n & 1) == 0 matches n % 2 == 0. Prefer % for readability unless a profiler proves otherwise.

Interview tip: mention 0, show you know inclusive ranges, and acknowledge how % behaves for negative odds.

❓ FAQ

An integer is even when you can split it into whole pairs with nothing left over. Formally, n is even if n = 2k for some integer k—examples include ..., -4, -2, 0, 2, 4, ...
Yes. Zero equals 2 times zero. In C#, the test number % 2 == 0 returns true when number is 0.
The % operator gives the remainder after division. When you divide by 2, even numbers leave remainder 0 and odd numbers leave a nonzero remainder.
Often yes: (n & 1) == 0 means the least significant bit is 0, which matches evenness for typical int values. Use % first while learning; mention bitwise only if you understand two's complement edge cases.
C# remainder follows the rule (a / b) * b + (a % b) == a with / truncating toward zero. Even negatives like -4 still satisfy n % 2 == 0. For odd negatives, the remainder may be -1 rather than +1.
Testing one integer is O(1) time and O(1) extra space. Printing every even value between start and end needs O(end - start + 1) steps in the simple loop.

🔄 Input / output examples

For Example 1, change the literal number or replace it with int.Parse(Console.ReadLine()) (wrapped in validation if users type garbage). Example 2 adjusts start and end.

Value of nIsEven(n) in C#
0true (zero is even)
10true
11false
-4true (remainder 0)

Edge cases and pitfalls

Even though the math is tiny, interviewers like hearing that you notice boundaries, signs, and loop conditions.

Zero

n = 0

Zero is even; 0 % 2 == 0 is true. Do not treat “not positive” as “not even.”

Negatives

Odd negatives

For odd negatives such as -3, C# yields remainder -1. The evenness test still works: -3 % 2 == 0 is false.

Bounds

Inclusive ranges

Use i <= end when the problem statement includes the endpoints; i < end accidentally drops the last number.

Types

int.MinValue

The smallest int is even (it equals 2 times another integer). Bitwise parity tricks assume two’s complement semantics—still fine on normal .NET runtimes.

⏱️ Time and space complexity

OperationTimeExtra space
IsEven(n)O(1)O(1)
Range [start, end] with per-value testO(end - start + 1)O(1)
Step-by-two loop over evens onlyabout half as many iterationsO(1)

No auxiliary collections are required—only a handful of integers and bool flags.

Summary

  • Definition: even integers are multiples of 2; check with n % 2 == 0.
  • Structure: wrap the test in IsEven, branch or loop from Main, keep ranges inclusive when asked.
  • Extras: zero counts as even; negatives follow C# remainder rules; bitwise parity is optional polish.
Did you know?

In modern mathematics zero is even: it fits the rule n = 2k with k = 0. In C#, 0 % 2 == 0 is true, so zero counts as even.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful