- Picture it
- four pairs of items with none left over
- Code check
8 % 2 == 0
Check Even Number in C#
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 smallIsEvenhelper that returnstrueorfalse. - How to reuse the same helper inside a loop to print every even value in a range such as
1through10. - 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 integerforloops. - 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.
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.
1010 ÷ 2 = 5 with remainder 0, so 10 is even. In C# you would see 10 % 2 == 0 evaluate to true.
Intuition
- Remainder
7 % 2is1in 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.
- Type an integer (try 10, 0, 11, or -4).
- Press Check parity or hit Enter.
- Read the remainder line and the plain-language sentence.
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
function isEven(n):
return (n mod 2) = 0
function printEvensInRange(start, end):
for i from start to end:
if isEven(i):
output i 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.
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.
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.
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
🔄 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 n | IsEven(n) in C# |
|---|---|
0 | true (zero is even) |
10 | true |
11 | false |
-4 | true (remainder 0) |
Edge cases and pitfalls
Even though the math is tiny, interviewers like hearing that you notice boundaries, signs, and loop conditions.
n = 0
Zero is even; 0 % 2 == 0 is true. Do not treat “not positive” as “not even.”
Odd negatives
For odd negatives such as -3, C# yields remainder -1. The evenness test still works: -3 % 2 == 0 is false.
Inclusive ranges
Use i <= end when the problem statement includes the endpoints; i < end accidentally drops the last number.
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
| Operation | Time | Extra space |
|---|---|---|
IsEven(n) | O(1) | O(1) |
Range [start, end] with per-value test | O(end - start + 1) | O(1) |
| Step-by-two loop over evens only | about half as many iterations | O(1) |
No auxiliary collections are required—only a handful of integers and bool flags.
Summary
- Definition: even integers are multiples of
2; check withn % 2 == 0. - Structure: wrap the test in
IsEven, branch or loop fromMain, keep ranges inclusive when asked. - Extras: zero counts as even; negatives follow C# remainder rules; bitwise parity is optional polish.
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.
8 people found this page helpful
