Hero Background

Next-Gen App & Browser Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud

Java Program to Check Odd or Even Number

To check whether a number is odd or even in Java, divide it by 2 using the modulus operator and inspect the remainder. If num % 2 equals 0, the number is even; otherwise it is odd. You can also use the bitwise AND operator for a faster check. The examples below show both approaches with complete, runnable code.

Understanding Odd and Even Numbers

An even number is divisible by 2 and leaves a remainder of 0, such as 0, 2, 4, 6, and 8, along with every number ending in those digits. An odd number leaves a remainder of 1 and is not divisible by 2, such as 1, 3, 5, 7, and 9. In Java, this parity check is one of the most common beginner exercises and a frequent interview warm-up, often paired with an if-else statement.

Method 1: Using the Modulus Operator

The modulus operator (%) returns the remainder of a division. It is the clearest and most readable way to test parity. The program below reads a number and prints whether it is odd or even:

import java.util.Scanner;

public class OddEven {
    public static void main(String[] args) {
        int num;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number you want to check: ");
        num = sc.nextInt();
        if (num % 2 == 0) {
            System.out.println("The entered number " + num + " is Even");
        } else {
            System.out.println("The entered number " + num + " is Odd");
        }
        sc.close();
    }
}

Sample output: entering 7 prints "The entered number 7 is Odd", while entering 10 prints "The entered number 10 is Even".

Method 2: Using the Bitwise AND Operator

Every even number has its least significant bit set to 0, and every odd number has it set to 1. The bitwise AND operator (&) inspects that bit directly, which is faster than performing a division:

public class OddEvenBitwise {
    public static void main(String[] args) {
        int num = 15;
        if ((num & 1) == 0) {
            System.out.println(num + " is Even");
        } else {
            System.out.println(num + " is Odd");
        }
    }
}

Method 3: Using a Ternary Operator

For a concise one-line check, the ternary operator assigns the result without a full if-else block:

public class OddEvenTernary {
    public static void main(String[] args) {
        int num = 8;
        String result = (num % 2 == 0) ? "Even" : "Odd";
        System.out.println(num + " is " + result);
    }
}

Method 4: Finding Odd or Even Numbers in a Range

To list every even number within a range, combine a for loop with the modulus check:

public class EvenInRange {
    public static void main(String[] args) {
        int start = 1, end = 10;
        System.out.print("Even numbers: ");
        for (int i = start; i <= end; i++) {
            if (i % 2 == 0) {
                System.out.print(i + " ");
            }
        }
    }
}

Common Mistakes and Troubleshooting

  • Using == 1 for odd with negatives: Writing num % 2 == 1 fails for negative odd numbers, since -3 % 2 is -1 in Java. Test for != 0 instead.
  • Confusing = and ==: Using a single equals sign is an assignment, not a comparison, and will not compile inside the condition.
  • Forgetting to close the Scanner: Leaving the Scanner open can trigger a resource leak warning; call sc.close() when done.
  • Assuming 0 is odd: Zero is even because 0 % 2 is 0. Do not special-case it.
  • Wrong data type for large input: Use long instead of int when values may exceed the int range to avoid overflow.

Conclusion

Checking whether a number is odd or even in Java is straightforward: the modulus operator is the most readable choice, while the bitwise AND operator offers a faster alternative. Understanding both, along with the ternary shorthand and range loops, builds a solid foundation for more advanced logic. For more practice, explore our collection of Java interview questions.

Frequently Asked Questions

How do you check if a number is odd or even in Java?

The simplest way is the modulus operator. If num % 2 equals 0, the number is even; otherwise it is odd. You place this condition inside an if-else statement and print the appropriate result.

Can you check odd or even in Java without the modulus operator?

Yes. You can use the bitwise AND operator: if (num & 1) equals 0 the number is even, otherwise it is odd. This checks the least significant bit and is generally faster than modulus.

Why is the modulus operator used to find even numbers?

The modulus operator returns the remainder of a division. Dividing an even number by 2 leaves a remainder of 0, while an odd number leaves a remainder of 1, making it a clear and readable test for parity.

How do you find all even numbers in a range in Java?

Use a for loop that iterates over the range and apply the modulus or bitwise check to each value. Print the numbers where the condition for even is true to list every even number in that range.

Is 0 considered an even number in Java?

Yes. Zero is even because 0 % 2 equals 0, so any odd/even program will correctly classify 0 as even. Negative numbers also work, since -4 % 2 is 0 in Java.

Which is faster, modulus or bitwise, for checking parity?

The bitwise AND approach is typically faster because it inspects a single bit rather than performing a division. For most applications the difference is negligible, so readability of the modulus method is usually preferred.

Related Questions

Test Your Website on 3000+ Browsers

Get 100 minutes of automation test minutes FREE!!

Test Now...

KaneAI - Testing Assistant

World’s first AI-Native E2E testing agent.

...

TestMu AI forEnterprise

Get access to solutions built on Enterprise
grade security, privacy, & compliance

  • Advanced access controls
  • Advanced data retention rules
  • Advanced Local Testing
  • Premium Support options
  • Early access to beta features
  • Private Slack Channel
  • Unlimited Manual Accessibility DevTools Tests