Check Odd Number in Java

Beginner
⏱️ 9 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Parity

What You’ll Learn

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.

Definition

Not divisible by 2

Odd integers have nonzero remainder mod 2.

Test

n % 2 != 0

One modulo check decides parity.

Zero

Even

0 % 2 = 0, so zero is not odd.

Range List

1..10

Print odds with a loop + helper.

Live Preview

Try 15 / 0 / -3

Check any integer in the browser.

O(1) Check

One compare

A single modulo decides yes or no.

Introduction

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.

Why it matters?

Parity checks appear constantly in interviews and everyday logic — and they are the twin of even-number tests.

Key Highlights

Modulo Test

n % 2 != 0 means odd.

Boolean Helper

Keep logic separate from printing.

Zero Is Even

Odd check returns false for 0.

Step by 2

List odds without testing every n.

In short: return n % 2 != 0; reuse that helper when scanning a range.

📝 Problem & Approach

Given an integer, decide whether it is odd, and optionally list all odd values in a closed range.

java
// 15 % 2 = 1  -> odd
//  8 % 2 = 0  -> not odd (even)
//  0 % 2 = 0  -> not odd (even)

Inputs & Outputs

ItemTypeDescription
number / nintInteger to classify.
Returnbooleantrue when n % 2 != 0.
Range printtextOdd integers from start through end.

Minimal workflow

Pseudocode
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 comparison

MethodIdeaNotes
Modulon % 2 != 0Interview default — clearest
Bitwise(n & 1) == 1Fine optional; explain modulo first
Step by 2for (int i = startOdd; i <= end; i += 2)Lists odds without testing each n

⚡ Quick Reference

GoalPattern
Check oddreturn n % 2 != 0;
Check evenreturn n % 2 == 0;
Messageif (isOdd(n)) System.out.println(...)
Scan rangefor (int i = start; i <= end; i++)
Step by 2for (int i = 1; i <= 10; i += 2)
Bit trick(n & 1) == 1

📋 Modulo vs Bitwise vs Step-2

Same parity answer — different styles and interview signals.

Modulo
n % 2 != 0

This page — clearest for beginners

Bitwise
(n & 1) == 1

Optional; mention after modulo

Step by 2
i += 2

Efficient listing of odds only

Interview tip
zero is even

State the zero edge case up front

Context

When This Problem Shows Up

Reach for an odd check whenever you need nonzero remainder mod 2.

  1. Interview warm-ups

    Modulo, helpers, and zero discussion.

  2. Filtering loops

    Keep only odd indices or values.

  3. Opposite of even

    Same skill with flipped comparison.

  4. Teaching %

    First clear use of the modulo operator.

  5. Not for floats

    Parity is defined for integers.

Key benefit: one comparison that locks in modulo thinking, zero handling, and range filtering.

🔮 Live Preview

Uses JavaScript safe integers but follows the same odd-number rule as the Java examples.

Try 0, 7, 22, or -3.

Live result
Press “Is it odd?” to see the verdict.

Examples Gallery

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.

📚 Getting Started

A reusable helper and one sample value.

Example 1 — Check One Number

Simple helper using modulo to classify a single value.

java
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.");
        }
    }
}

How It Works

15 % 2 equals 1, so the helper returns true. The caller turns that boolean into a readable sentence.

⚡ Listing Odds

Reuse the same helper while scanning a range.

Example 2 — Odds in [1, 10]

Loop through the range and print values that pass the odd check.

java
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();
    }
}

How It Works

The loop uses i <= 10 so 10 is included. Only values that pass isOdd are printed.

Example 3 — Step by Two

Start at the first odd and increment by 2 — no per-value modulo needed.

java
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);
    }
}

How It Works

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.

🧠 How the Algorithm Decides

1

Take an integer

Use a fixed value or validated input.

Input
2

Compute n % 2

Remainder when dividing by 2.

Modulo
3

Test nonzero

If remainder != 0, the number is odd.

Rule
=

Print or filter

Reuse the same helper in range loops.

🔎 Worked Walkthrough — Sample Values

Apply n % 2 != 0 to a few integers.

nn % 2Odd?
151Yes
80No
00No (even)
-5-1Yes
220No

Example 1 prints that 15 is an odd number.

Use Cases

Where odd-number checks show up beyond the interview prompt.

1. Interview Warm-Ups

Modulo and boolean helpers.

Example: write isOdd.

2. Filtering Ranges

Print or collect only odds.

Example: 1 3 5 7 9.

3. Opposite of Even

Flip == 0 to != 0.

Example: twin of isEven.

4. Index Patterns

Process every other item.

Example: odd indices.

5. Zero Edge Talk

Show that 0 is even, not odd.

Example: 0 % 2 = 0.

6. Bridge to Palindrome

Next number-classification topic.

Example: related CTA.

Pro Tip: open with “odd means n % 2 != 0; zero is even” before writing code.

Advantages

Why the modulo-based odd check works well for beginners and interviews.

  1. 1. Tiny Logic

    One remainder decides the answer.

  2. 2. Reusable Helper

    Boolean return keeps printing and logic separate.

  3. 3. Works for Negatives

    The nonzero-remainder rule still classifies negatives correctly.

  4. 4. Cheap

    O(1) time and space for a single check.

Pro Tip: explain modulo first; mention (n & 1) only as an optional aside.

Usage Tips

Small habits that keep odd-number solutions interview-ready.

  1. 1. Prefer a Boolean Helper

    Return true/false; print in the caller.

  2. 2. Mention Zero

    Say explicitly that zero is even.

  3. 3. Watch range Endpoints

    Use i <= end when you need an inclusive end.

  4. 4. Step by 2 for Long Lists

    Avoid testing every integer when you only need odds.

  5. 5. Lead With Modulo

    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.

Common Pitfalls

Mistakes that commonly break odd-number programs.

  1. 1. Calling Zero Odd

    Assuming 0 fails evenness somehow.

    → 0 % 2 = 0, so zero is even.

  2. 2. Wrong Comparison

    Using == 0 when you meant odd.

    → Odd needs nonzero remainder.

  3. 3. Off-by-One range

    Missing the last value with exclusive end.

    → Use for (int i = start; i <= end; i++).

  4. 4. Treating Floats as Parity

    Asking if 1.5 is odd.

    → Stick to integers.

  5. 5. Skipping Negatives

    Assuming only positives can be odd.

    → Test -5 in your walkthrough.

Edge Cases

Odd/even classification works for positive, zero, and negative integers.

Zero

n = 0

Zero is even, so odd check returns false.

Negative

Still valid

Example: -5 % 2 is -1 in Java, so -5 is odd.

Range

Inclusive end

Use i <= end in the loop so the last value is included.

One

n = 1

Smallest positive odd integer.

Even

Opposite parity

If not odd, it is even for integers.

Float

Decimals

Out of scope — parity is for integers.

⚖️ Facts Worth Knowing

Handy follow-ups interviewers sometimes ask.

  • Exclusive classes. Every integer is odd or even, never both.
  • Zero is even. 0 = 2×0, so it fails the odd test.
  • Successor flips. n odd ⇒ n+1 even; n even ⇒ n+1 odd.
  • Bit view. The least significant bit is 1 for odd integers.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Classify four values

  • Test 15, 0, 22, -5
  • Match the walkthrough table

2. Print 1..10 odds

  • Reproduce Example 2
  • Expect 1 3 5 7 9

3. Step-by-two version

  • Implement Example 3
  • Start odd, step 2

4. Twin even helper

  • Write is_even with == 0
  • Confirm opposite of isOdd

Notes

  • Test: n % 2 != 0.
  • Zero is even, so it is not odd.
  • Ranges: reuse the same helper in loops.
  • For long ranges, start at the first odd and increment by 2. (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.

⏱️ Time and Space Complexity

OperationTimeExtra space
isOdd(n)O(1)O(1)
Range [a, b] scanO(b - a + 1)O(1)
Step-by-2 listingO((b - a) / 2)O(1)

One comparison is constant time; listing grows with how many numbers you visit.

Wrap Up

🎉 Conclusion

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.

💡 Best Practices

✅ Do

  • Use n % 2 != 0
  • Keep a boolean helper
  • State that zero is even
  • Test negatives too
  • Step by 2 for long odd lists

❌ Don’t

  • Call zero odd
  • Use == 0 for odd checks
  • Forget inclusive loop bounds
  • Apply parity to floats
  • Lead with bitwise only

Key Takeaways

Knowledge Unlocked

Five things to remember about odd numbers

Classify parity the interview-friendly way.

5
Core concepts
? 02

Helper

Return boolean

Pattern
0 03

Zero

Even, not odd

Edge
2 04

Step

i += 2

List
O 05

Cost

O(1) check

Analysis

❓ Frequently Asked Questions

It is an integer that is not divisible by 2. For nonnegative values, this means remainder 1 when divided by 2.
No. Zero is even, because 0 % 2 equals 0.
Because remainder by 2 directly tells parity. Nonzero remainder means odd.
Yes, for integers. Exactly one of odd/even is true.
Yes for integer bit checks, but modulo is usually clearer for beginners.
Single check is O(1). Scanning a range is O(range size).
Yes. In Java, -5 % 2 equals -1, which is still nonzero, so -5 is odd under n % 2 != 0.
Start at the first odd value and step by 2 instead of testing every integer.
Even uses n % 2 == 0. Odd uses nonzero remainder — opposite parity.

Did you Know? 🔊

Every whole number is either even or odd—never both. Zero is even, so the test n % 2 != 0 correctly says zero is not odd.

Continue to Palindrome Number

Learn how to check whether a number reads the same forwards and backwards in Java.

Palindrome number tutorial →

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