Next-Gen App & Browser Testing Cloud
Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

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