Check Even Number in C++

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

What you’ll learn

  • The definition of even (including zero) and the usual C test n % 2 == 0.
  • A tiny isEven helper plus printing evens in a closed interval [start, end].
  • Live preview and notes on signed remainder rules in C++.

Overview

Parity splits the integers into evens and odds. The simplest C predicate is number % 2 == 0, which returns a truth value you can pass to if or aggregate in a loop.

Two programs

One value (10) and a range 1–10 listing.

Live preview

Safe-range integers; shows parity with the same rule as the C samples.

Rigor

Explicit note on zero and C % with negatives.

Prerequisites

The modulo operator %, if, and for loops.

  • #include <iostream>, int main(), std::cout.
  • Optional: bitwise & as an alternative parity test.

What is an even number?

An integer n is even iff n = 2k for some integer k. On the number line, evens are spaced by two: …, -4, -2, 0, 2, 4, ….

Checking divisibility by 2 is the standard program task; it is the same predicate used inside many “print every other element” loops.

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

Congruence mod 2

Even integers are exactly those congruent to 0 modulo 2: n ≡ 0 (mod 2). Odd integers are congruent to 1 (mod 2) in the usual residue system.

10

10 = 2 · 5, remainder 0 upon division by 2.

Intuition

8 Even
Pairs
four groups of two
7 Odd
Remainder
7 % 2 = 1

Takeaway: parity is the first nontrivial modular filter after divisibility by every integer.

Live preview

Integers in the JavaScript safe range. Uses n % 2 === 0 (same idea as the C code).

Try 0, -4, or 11.

Live result
Press “Check parity” to classify.

Algorithm

Goal: return true iff n is divisible by 2.

Compute n % 2

If the remainder is 0, n is even; otherwise odd (for signed n, mind C remainder sign rules).

Range scan

For each i in [start, end], apply the same predicate and print matches.

📜 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

n % 2 == 0

Minimal helper returning a boolean as int (nonzero vs zero), matching the reference style.

c++
#include <iostream>

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

int main() {
    int number = 10;

    if (isEven(number)) {
        std::cout << number << " is an even number.\n";
    } else {
        std::cout << number << " is not an even number.\n";
    }

    return 0;
}

Explanation

In C++, isEven returns 1 or 0; if (isEven(number)) branches on nonzero.

2

Evens in [1, 10]

Same output as the reference range demo: 2 4 6 8 10. Reuses n % 2 == 0 inside the loop.

c++
#include <iostream>

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

void checkEvenNumbersInRange(int start, int end) {
    std::cout << "Even numbers in the range " << start << " to " << end << ":\n";

    for (int i = start; i <= end; ++i) {
        if (isEven(i)) {
            std::cout << i << " ";
        }
    }

    std::cout << "\n";
}

int main() {
    int start = 1;
    int end = 10;

    checkEvenNumbersInRange(start, end);
    return 0;
}

Explanation

The loop walks every integer in the inclusive range; isEven filters to multiples of two. An alternative micro-optimization is (i & 1) == 0 on two's complement.

Optimization

Step by two. If you only need evens, iterate for (i = start + (start&1); i <= end; i += 2) after aligning start to the next even.

Bitwise parity. (n & 1) == 0 matches n % 2 == 0 for typical two's complement integers; use when profiling shows it matters.

Interview: mention 0, C++ remainder sign rules, and the bitwise alternative.

❓ FAQ

An integer n is even if there exists an integer k with n = 2k. Equivalently, n is divisible by 2 with remainder 0.
Yes: 0 = 2·0. The test n % 2 == 0 returns true for n = 0 in C++.
The remainder of division by 2 is 0 for even integers and nonzero (±1 in C++, depending on sign rules) for odd integers when using signed types.
On two's complement, (n & 1) == 0 detects even integers for typical signed and unsigned widths. It is a common micro-optimization; parity via % is clearer for beginners.
For negative n, modern C++ defines a/b truncating toward zero and a % b so that (a/b)*b + a%b == a. Even negatives like -4 still satisfy n % 2 == 0; odd negatives yield a nonzero remainder with the same sign as the dividend.
A single test is O(1) time and O(1) space. Scanning a range [a,b] is O(b - a + 1) with O(1) extra space.

🔄 Input / output examples

Change number in Example 1 or start/end in Example 2.

nEven?
0Yes
10Yes
11No
-4Yes (C remainder 0)

Edge cases and pitfalls

Treating “not even” as always meaning “odd” is fine for nonnegative integers; for negative odds, % may show a remainder of -1 in C++.

Zero

n = 0

Even by definition; 0 % 2 == 0 is true.

INT_MIN

Two's complement

INT_MIN is even; bitwise parity still matches modular parity on two's complement machines.

Types

unsigned

If you only care about nonnegative values, unsigned avoids negative remainder sign surprises in pedantic proofs.

Off-by-one

Inclusive range

Use i <= end when the problem says “through end” inclusively.

⏱️ Time and space complexity

OperationTimeExtra space
isEven(n)O(1)O(1)
Range [a, b]O(b - a + 1)O(1)
Step-by-two loopO((b-a)/2) iterationsO(1)

No auxiliary memory is required beyond loop indices.

Summary

  • Test: n % 2 == 0 or (n & 1) == 0.
  • Zero is even; range scans reuse the same predicate.
  • Watch-outs: C++ remainder sign on negatives; inclusive loop bounds.
Did you know?

In modern mathematics zero is even: it is an integer multiple of 2 (0 = 2 · 0). The expression n % 2 == 0 in C is true for n = 0.

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