Check Odd Number in Java

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

What you’ll learn

  • How to check odd numbers using n % 2 != 0.
  • How this rule works for positive, zero, and negative integers.
  • How to print odd numbers in a range with a simple loop.

Prerequisites

Integers, modulo operator, and simple if-else flow.

  • Java int and % basics.
  • Parity idea: every integer is either even or odd.

What is an odd number?

An integer is odd when divided by 2 gives a non-zero remainder. In Java, use n % 2 != 0.

Math definition

An integer n is odd iff n = 2k + 1 for some integer k. Equivalent program check: n % 2 != 0.

Intuition and examples

15 % 2 = 1 so odd; 18 % 2 = 0 so not odd.

Live preview

Live result
Press "Is it odd?" to check.

Algorithm

Compute n % 2; non-zero means odd.

📜 Pseudocode

Pseudocode
if n % 2 != 0:
  odd
else:
  not odd
1

Check one number

java
public class Main {
    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.");
        }
    }
}
2

Print odds from 1 to 10

java
public class Main {
    static void printOddNumbersFrom1To10() {
        System.out.println("Odd numbers in the range 1 to 10:");
        for (int i = 1; i <= 10; i++) {
            if (i % 2 != 0) {
                System.out.print(i + " ");
            }
        }
        System.out.println();
    }

    public static void main(String[] args) {
        printOddNumbersFrom1To10();
    }
}

Optimization

Single parity check is already constant-time; focus on readability.

❓ FAQ

A whole number not divisible by 2 is odd.
No. Zero is even.
Because odd numbers leave nonzero remainder when divided by 2.
Yes. Negative odd numbers also satisfy n % 2 != 0.
For integers, yes.
Single check O(1); scanning range O(k).

🔄 Input / output examples

n = 15 -> odd, n = 0 -> not odd, n = -3 -> odd.

Edge cases

Zero is even; negative odd numbers are valid odd integers.

⏱️ Time and space complexity

Single check O(1), range print O(k).

Summary

  • Use n % 2 != 0 for odd check in Java.
  • Runs in constant time for one number.
Did you know?

Every integer is either even or odd. Zero is even, so it is not odd.

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