Definition
Not divisible by 2
Odd integers leave a nonzero remainder when divided by 2.
An integer is odd when it is not divisible by 2 — in PHP, when $n % 2 != 0. This tutorial covers the modulo test, why zero is not odd, range listing, a live preview, worked PHP examples, edge cases, and complexity.
Not divisible by 2
Odd integers leave a nonzero remainder when divided by 2.
n % 2 != 0
One remainder check classifies the number.
Even
0 is even, so isOdd(0) is false by design.
1..10
Print 1 3 5 7 9 from a simple loop.
Try values
Check 15, 0, or -3 under the same rule.
O(k) range
One test is constant; scanning a range is linear.
Odd numbers are the other half of the integers alongside evens. If splitting a pile into two equal whole rows leaves exactly one left over, the count is odd.
In PHP you detect that with the remainder of division by 2: $n % 2 != 0. Zero is even (0 = 2 · 0), so it correctly fails the odd test.
Parity checks are classic interview and homework warm-ups: modulo, booleans, and the easy-to-miss fact that zero is even.
$n % 2 != 0
So it is not odd — by design.
isOdd and isEven partition the integers.
List odds with i += 2 after aligning start.
In short: return true when $n % 2 != 0; remember zero is even; reuse the same test inside range loops.
Given an integer, decide whether it is odd using remainder modulo 2. Optionally list all odds in a closed range.
// 15 % 2 = 1 -> odd
// 14 % 2 = 0 -> not odd
// 0 % 2 = 0 -> not odd (even) | Item | Type | Description |
|---|---|---|
$number | int | Integer to classify. |
| Return | bool | true if odd under % 2 != 0. |
| Printed output | text | Yes/no sentence, or listed odds in a range. |
function isOdd(n):
return (n mod 2) != 0
function printOddsInRange(start, end):
for i from start to end:
if isOdd(i):
output i | Method | Idea | Notes |
|---|---|---|
| Modulo | $n % 2 != 0 | Clearest for beginners |
| Bit test | ($n & 1) != 0 | Common micro-optimization |
| Step-by-two list | for ($i = $oddStart; $i <= $end; $i += 2) | Fewer iterations when listing odds |
| Goal | Pattern |
|---|---|
| Is odd? | return $n % 2 != 0; |
| Is even? | return $n % 2 == 0; |
| Bit odd test | return ($n & 1) != 0; |
| Print message | echo $number . " is an odd number.\n"; |
| Filter in range | if ($i % 2 != 0) echo $i . " "; |
| Step by two | for ($i = 1; $i <= 10; $i += 2) |
Same odd numbers — different styles and trade-offs.
% 2 != 0This page — clearest interview default
& 1Fine later; mention after you know %
i += 2Lists odds without testing evens
mention 0Zero is even — say it out loud
Reach for an odd check whenever parity or remainder-by-2 matters.
Modulo, booleans, and the zero edge case.
Flip != to == for the opposite check.
Print only odds (or skip them) in loops.
Remainder is easier to see with a leftover of 1.
Parity is an integer idea — cast or validate first.
Key benefit: one comparison that locks in modulo thinking and the even/odd partition of the integers.
Uses JavaScript safe integers. The rule matches the PHP idea: odd when n % 2 !== 0.
Three complete PHP programs — single odd check for 15, print odds in 1..10, and a step-by-two range list. Click View Output to reveal sample console results.
A reusable helper and one sample value.
n % 2 != 0Helper returns a boolean; sample value 15 with a clear yes/no line.
<?php
function isOdd(int $number): bool
{
return $number % 2 != 0;
}
$number = 15;
if (isOdd($number)) {
echo $number . " is an odd number.\n";
} else {
echo $number . " is not an odd number.\n";
}
?> 15 = 2 · 7 + 1, so the remainder is 1 and isOdd is true. Change $number to 14 or 0 to see the else branch.
Reuse the same remainder test inside a for loop.
[1, 10]Walk the closed range and print 1 3 5 7 9.
<?php
function printOddNumbersFrom1To10(): void
{
echo "Odd numbers in the range 1 to 10:\n";
for ($i = 1; $i <= 10; $i++) {
if ($i % 2 != 0) {
echo $i . " ";
}
}
echo "\n";
}
printOddNumbersFrom1To10();
?> Each integer is tested once; only nonzero remainders are printed. Keep $i <= 10 so both endpoints stay inclusive.
Advance by 2 after starting on an odd value.
Same printed odds as Example 2, with half as many loop iterations.
<?php
function printOddsStepByTwo(int $start, int $end): void
{
if ($start % 2 == 0) {
$start++;
}
echo "Odd numbers from $start toward $end (step 2):\n";
for ($i = $start; $i <= $end; $i += 2) {
echo $i . " ";
}
echo "\n";
}
printOddsStepByTwo(1, 10);
?> If $start is even, bump it to the next odd, then add 2 each time. You never visit an even integer in the loop body.
Use a literal or a validated CLI value.
If the remainder is not 0, the number is odd.
Print a yes/no sentence, or list matching range values.
The integer is labeled odd or not odd.
Apply $n % 2 != 0 to a few integers.
$n | $n % 2 | Odd? |
|---|---|---|
15 | 1 | Yes |
14 | 0 | No |
0 | 0 | No (even) |
-5 | nonzero | Yes |
For nonnegative beginners, “remainder 1” is the usual mental picture of odd.
Where odd-number checks show up beyond the interview prompt.
Modulo and boolean helpers.
Example: write isOdd($n).
Same idea with the flipped comparison.
Example: even uses == 0.
Print or skip odds in a loop.
Example: Example 2 pattern.
Leftover 1 is easy to picture.
Example: 15 = 2·7 + 1.
Show that 0 is even, not odd.
Example: isOdd(0) is false.
List odds with i += 2.
Example: Example 3 pattern.
Pro Tip: open with “odd means remainder nonzero mod 2, and zero is even” before writing code.
Why the modulo odd check works well in interviews.
A single remainder comparison encodes the definition.
Even is the same idea with == 0.
Reuse isOdd inside any loop filter.
O(1) time and space for a single classification.
Pro Tip: keep isOdd boolean and put wording in the echo layer.
Small habits that keep odd-number solutions interview-ready.
Use modulo until bit tricks feel natural.
State that 0 is even before coding.
Return true/false; print messages outside.
Use <= when the prompt includes both ends.
For odd-only loops, bump even starts before += 2.
Pro Tip: dry-run 15, 14, and 0 aloud — those three catch almost every beginner mistake.
Mistakes that commonly break odd-number solutions.
Thinking 0 has no parity or is somehow special.
→ Zero is even; 0 % 2 == 0.
Copying the even test without flipping the comparison.
→ Odd needs != 0.
Using < when the prompt includes the end value.
→ Prefer <= for closed ranges like 1 to 10.
Starting an even $start and adding 2 forever.
→ Bump even starts to the next odd first.
Passing decimals into an int helper silently.
→ Validate whole numbers on CLI paths.
The phrase “not odd” includes evens and zero — do not confuse it with “even and positive.”
n = 00 % 2 == 0, so isOdd(0) is false.
Odd negatives still satisfy n % 2 != 0 in PHP.
n = 1Smallest positive odd integer.
i <= endKeep both endpoints for “from 1 to 10.”
Bump even starts before adding 2.
Loop never runs; print nothing.
Handy follow-ups interviewers sometimes ask.
Try these variations to lock in the pattern.
$n % 2 != 0 (or ($n & 1) != 0 once you are ready).for loop.Quick Takeaway: odd means $n % 2 != 0 — and zero is not odd.
| Operation | Time | Extra space |
|---|---|---|
isOdd(n) | O(1) | O(1) |
Range [a, b] | O(b - a + 1) | O(1) |
| Step-by-two odd loop | about half as many iterations | O(1) |
No heap allocation is required for these snippets.
Checking an odd number is a one-line remainder test: return true when $n % 2 != 0. Reuse that helper for range listing, or step by two after aligning the start, and always remember that zero is even.
Practice the three examples above, then continue to palindrome numbers.
Odd means nonzero remainder mod 2 — say zero is even before you code.
$n % 2 != 0 for clarityisOdd booleanClassify parity the interview-friendly way.
% 2 != 0
RuleEven, not odd
EdgeisOdd bool
CodeFilter or +2
PatternO(1) / O(k)
AnalysisEvery whole number is either even or odd—never both, never neither. Zero is even, so the test n % 2 != 0 correctly says zero is not odd.
Learn how to check whether a number reads the same forwards and backwards in PHP.
8 people found this page helpful