- Pairs
- four groups of two
Check Even Number in C++
What you’ll learn
- The definition of even (including zero) and the usual C test
n % 2 == 0. - A tiny
isEvenhelper 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.
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.
1010 = 2 · 5, remainder 0 upon division by 2.
Intuition
- 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).
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
function isEven(n):
return (n mod 2) = 0
function printEvensInRange(start, end):
for i from start to end:
if isEven(i):
output i n % 2 == 0
Minimal helper returning a boolean as int (nonzero vs zero), matching the reference style.
#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.
Evens in [1, 10]
Same output as the reference range demo: 2 4 6 8 10. Reuses n % 2 == 0 inside the loop.
#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
🔄 Input / output examples
Change number in Example 1 or start/end in Example 2.
| n | Even? |
|---|---|
0 | Yes |
10 | Yes |
11 | No |
-4 | Yes (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++.
n = 0
Even by definition; 0 % 2 == 0 is true.
Two's complement
INT_MIN is even; bitwise parity still matches modular parity on two's complement machines.
unsigned
If you only care about nonnegative values, unsigned avoids negative remainder sign surprises in pedantic proofs.
Inclusive range
Use i <= end when the problem says “through end” inclusively.
⏱️ Time and space complexity
| Operation | Time | Extra space |
|---|---|---|
isEven(n) | O(1) | O(1) |
Range [a, b] | O(b - a + 1) | O(1) |
| Step-by-two loop | O((b-a)/2) iterations | O(1) |
No auxiliary memory is required beyond loop indices.
Summary
- Test:
n % 2 == 0or(n & 1) == 0. - Zero is even; range scans reuse the same predicate.
- Watch-outs: C++ remainder sign on negatives; inclusive loop bounds.
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.
8 people found this page helpful
