Definition
Not divisible by 2
Odd integers have nonzero remainder mod 2.
An odd integer leaves a nonzero remainder when divided by 2: n % 2 != 0. This tutorial covers a reusable helper, listing odds in a range, stepping by two, a live checker, worked Java examples, edge cases, and complexity.
Not divisible by 2
Odd integers have nonzero remainder mod 2.
n % 2 != 0
One modulo check decides parity.
Even
0 % 2 = 0, so zero is not odd.
1..10
Print odds with a loop + helper.
Try 15 / 0 / -3
Check any integer in the browser.
One compare
A single modulo decides yes or no.
Odd numbers are integers not divisible by 2. In Java, that is a one-line test: number % 2 != 0.
Every integer is either even or odd — never both. Zero is even, so the odd check correctly returns false for 0. Negatives still work: for example, -5 % 2 is -1 in Java, which is nonzero, so -5 is odd.
Parity checks appear constantly in interviews and everyday logic — and they are the twin of even-number tests.
n % 2 != 0 means odd.
Keep logic separate from printing.
Odd check returns false for 0.
List odds without testing every n.
In short: return n % 2 != 0; reuse that helper when scanning a range.
Given an integer, decide whether it is odd, and optionally list all odd values in a closed range.
// 15 % 2 = 1 -> odd
// 8 % 2 = 0 -> not odd (even)
// 0 % 2 = 0 -> not odd (even) | Item | Type | Description |
|---|---|---|
number / n | int | Integer to classify. |
| Return | boolean | true when n % 2 != 0. |
| Range print | text | Odd integers from start through end. |
function isOdd(n):
return (n mod 2) != 0
function print_odds(start, end):
for i from start to end:
if isOdd(i):
output i | Method | Idea | Notes |
|---|---|---|
| Modulo | n % 2 != 0 | Interview default — clearest |
| Bitwise | (n & 1) == 1 | Fine optional; explain modulo first |
| Step by 2 | for (int i = startOdd; i <= end; i += 2) | Lists odds without testing each n |
| Goal | Pattern |
|---|---|
| Check odd | return n % 2 != 0; |
| Check even | return n % 2 == 0; |
| Message | if (isOdd(n)) System.out.println(...) |
| Scan range | for (int i = start; i <= end; i++) |
| Step by 2 | for (int i = 1; i <= 10; i += 2) |
| Bit trick | (n & 1) == 1 |
Same parity answer — different styles and interview signals.
n % 2 != 0This page — clearest for beginners
(n & 1) == 1Optional; mention after modulo
i += 2Efficient listing of odds only
zero is evenState the zero edge case up front
Reach for an odd check whenever you need nonzero remainder mod 2.
Modulo, helpers, and zero discussion.
Keep only odd indices or values.
Same skill with flipped comparison.
First clear use of the modulo operator.
Parity is defined for integers.
Key benefit: one comparison that locks in modulo thinking, zero handling, and range filtering.
Uses JavaScript safe integers but follows the same odd-number rule as the Java examples.
Three complete Java programs — a single-value check, odds in 1..10, and a step-by-two listing. Click View Output to reveal sample console results.
A reusable helper and one sample value.
Simple helper using modulo to classify a single value.
public class IsOdd {
static boolean isOdd(int number) {
return number % 2 != 0;
}
public static void main(String[] args) {
int number = 15;
if (isOdd(number)) {
System.out.println(number + " is an odd number.");
} else {
System.out.println(number + " is not an odd number.");
}
}
} 15 % 2 equals 1, so the helper returns true. The caller turns that boolean into a readable sentence.
Reuse the same helper while scanning a range.
Loop through the range and print values that pass the odd check.
public class Odds1To10 {
static boolean isOdd(int number) {
return number % 2 != 0;
}
static void printOddsFrom1To10() {
System.out.println("Odd numbers in the range 1 to 10:");
for (int i = 1; i <= 10; i++) {
if (isOdd(i)) {
System.out.print(i + " ");
}
}
System.out.println();
}
public static void main(String[] args) {
printOddsFrom1To10();
}
} The loop uses i <= 10 so 10 is included. Only values that pass isOdd are printed.
Start at the first odd and increment by 2 — no per-value modulo needed.
public class OddsStep2 {
static void printOddsStepByTwo(int start, int end) {
if (start % 2 == 0) {
start += 1;
}
System.out.println("Odd numbers from " + start + " stepping by 2 up to " + end + ":");
for (int i = start; i <= end; i += 2) {
System.out.print(i + " ");
}
System.out.println();
}
public static void main(String[] args) {
printOddsStepByTwo(1, 10);
}
} After aligning start to an odd value, every second integer is odd. This is useful for long ranges where you only need the odd sequence.
Use a fixed value or validated input.
Remainder when dividing by 2.
If remainder != 0, the number is odd.
Reuse the same helper in range loops.
Apply n % 2 != 0 to a few integers.
| n | n % 2 | Odd? |
|---|---|---|
15 | 1 | Yes |
8 | 0 | No |
0 | 0 | No (even) |
-5 | -1 | Yes |
22 | 0 | No |
Example 1 prints that 15 is an odd number.
Where odd-number checks show up beyond the interview prompt.
Modulo and boolean helpers.
Example: write isOdd.
Print or collect only odds.
Example: 1 3 5 7 9.
Flip == 0 to != 0.
Example: twin of isEven.
Process every other item.
Example: odd indices.
Show that 0 is even, not odd.
Example: 0 % 2 = 0.
Next number-classification topic.
Example: related CTA.
Pro Tip: open with “odd means n % 2 != 0; zero is even” before writing code.
Why the modulo-based odd check works well for beginners and interviews.
One remainder decides the answer.
Boolean return keeps printing and logic separate.
The nonzero-remainder rule still classifies negatives correctly.
O(1) time and space for a single check.
Pro Tip: explain modulo first; mention (n & 1) only as an optional aside.
Small habits that keep odd-number solutions interview-ready.
Return true/false; print in the caller.
Say explicitly that zero is even.
Use i <= end when you need an inclusive end.
Avoid testing every integer when you only need odds.
Save bitwise tricks for a follow-up comment.
Pro Tip: dry-run 15, 0, and -5 — if those three match the table, your rule is correct.
Mistakes that commonly break odd-number programs.
Assuming 0 fails evenness somehow.
→ 0 % 2 = 0, so zero is even.
Using == 0 when you meant odd.
→ Odd needs nonzero remainder.
Missing the last value with exclusive end.
→ Use for (int i = start; i <= end; i++).
Asking if 1.5 is odd.
→ Stick to integers.
Assuming only positives can be odd.
→ Test -5 in your walkthrough.
Odd/even classification works for positive, zero, and negative integers.
Zero is even, so odd check returns false.
Example: -5 % 2 is -1 in Java, so -5 is odd.
Use i <= end in the loop so the last value is included.
Smallest positive odd integer.
If not odd, it is even for integers.
Out of scope — parity is for integers.
Handy follow-ups interviewers sometimes ask.
Try these variations to lock in the pattern.
n % 2 != 0.(n & 1) == 1 also detects odd integers. Explain modulo first, then mention bitwise as optional.Quick Takeaway: odd means n % 2 != 0; zero is even; reuse the helper in range loops.
| Operation | Time | Extra space |
|---|---|---|
isOdd(n) | O(1) | O(1) |
Range [a, b] scan | O(b - a + 1) | O(1) |
| Step-by-2 listing | O((b - a) / 2) | O(1) |
One comparison is constant time; listing grows with how many numbers you visit.
Checking an odd number is a one-line rule: return n % 2 != 0. Keep the helper boolean, remember that zero is even, and reuse the same test when scanning ranges or stepping by two.
Practice the three examples above, then continue to checking palindrome numbers.
isOdd(n) returns n % 2 != 0; zero is not odd.
n % 2 != 0Classify parity the interview-friendly way.
n % 2 != 0
RuleReturn boolean
PatternEven, not odd
Edgei += 2
ListO(1) check
AnalysisEvery whole number is either even or odd—never both. 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 Java.
8 people found this page helpful