Introduction
Checking whether a number is even or odd is one of the first programming exercises every Java beginner encounters. Although the logic appears simple, it introduces several fundamental programming concepts such as the modulus operator, conditional statements, bitwise operations, and user input handling.
There are multiple ways to solve this problem in Java. While the modulus operator is the most common and readable approach, interviewers often ask for alternative solutions using bitwise operators or without using the modulus operator at all.
In this tutorial, you'll learn five different methods to check whether a number is even or odd, understand how Java evaluates each approach internally, and discover common interview questions and best practices.
What Does Even or Odd Mean?
An even number is an integer that is exactly divisible by 2, leaving no remainder.
Examples:
-8
-4
0
2
4
6
10
An odd number leaves a remainder when divided by 2.
Examples:
-7
-3
1
3
5
7
9
In Java, we usually determine this by checking the remainder after division by 2.
Method 1: Using the Modulus Operator (%)
The modulus operator returns the remainder after division.
If the remainder is 0, the number is even.
Otherwise, it is odd.
Java Program
public class Main {
public static void main(String[] args) {
int num = 13;
if (num % 2 == 0) {
System.out.println(num + " is an even number.");
} else {
System.out.println(num + " is an odd number.");
}
}
}
Output
13 is an odd number.
For:
int num = 8;
Output:
8 is an even number.
Time Complexity
O(1)
Space Complexity
O(1)
Method 2: Using the Bitwise AND Operator (&)
Sometimes interviewers ask you to solve the problem without using the modulus operator.
In that case, you can use the bitwise AND operator.
Java Program
public class Main {
public static void main(String[] args) {
int num = 13;
if ((num & 1) == 0) {
System.out.println(num + " is an even number.");
} else {
System.out.println(num + " is an odd number.");
}
}
}
Output
13 is an odd number.
Why Does It Work?
Every integer is stored in binary.
Even numbers always end with:
0
Odd numbers always end with:
1
The expression:
num & 1
extracts the last binary bit.
Example:
13
Binary
1101
1101
&
0001
=
0001
Result = 1
Odd
Example:
8
Binary
1000
1000
&
0001
=
0000
Result = 0
Even
Time Complexity
O(1)
Space Complexity
O(1)
Method 3: Using the Ternary Operator
For short conditions, Java's ternary operator provides a compact alternative.
Java Program
public class Main {
public static void main(String[] args) {
int num = 13;
String result = (num % 2 == 0) ? "even" : "odd";
System.out.println(num + " is an " + result + " number.");
}
}
Output
13 is an odd number.
The ternary operator is simply a shorter version of an if-else statement.
Time Complexity
O(1)
Space Complexity
O(1)
Method 4: Taking User Input Using Scanner
Instead of using a hardcoded value, you can read the number from the keyboard.
Java Program
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
System.out.println(
num % 2 == 0
? num + " is even."
: num + " is odd."
);
scanner.close();
}
}
Sample Output
Enter a number: 18
18 is even.
Time Complexity
O(1)
Space Complexity
O(1)
Method 5: Without Using Modulus or Bitwise Operators
If both % and & are prohibited, integer division can still be used.
Java Program
public class Main {
public static void main(String[] args) {
int num = 13;
int half = num / 2;
if (half * 2 == num) {
System.out.println(num + " is an even number.");
} else {
System.out.println(num + " is an odd number.");
}
}
}
Output
13 is an odd number.
How It Works
Integer division removes any remainder.
Example:
13 / 2
=
6
Multiply again:
6 × 2
=
12
Since:
12 ≠ 13
the number is odd.
For:
8 / 2 = 4
4 × 2 = 8
The original value is restored, so the number is even.
Time Complexity
O(1)
Space Complexity
O(1)
How Java Handles This Internally
Consider:
int num = 13;
Internally:
-
numis stored as a primitive integer in the stack memory. -
The JVM evaluates:
num % 2
-
The result is compared with:
0
-
The condition becomes either
trueorfalse. -
Java executes the appropriate branch of the
if-elsestatement. -
When
main()finishes, the stack frame is destroyed.
Except for the Scanner object in Method 4, no objects are created on the heap.
Real-Life Analogy
Imagine people standing in a line to form pairs.
If everyone can be paired, the number of people is even.
If one person is left without a partner, the number is odd.
The modulus operator performs exactly this check by determining whether anything is left over after grouping into pairs.
Comparison of Different Approaches
| Method | Readability | Performance | Best Use Case |
|---|---|---|---|
Modulus (%) |
⭐⭐⭐⭐⭐ | Excellent | Recommended for almost all programs |
Bitwise (&) |
⭐⭐⭐⭐ | Slightly faster | Interview questions and low-level programming |
Ternary (?:) |
⭐⭐⭐⭐⭐ | Same as modulus | Short, concise code |
| Scanner | ⭐⭐⭐⭐⭐ | Same as modulus | User-interactive programs |
| Division | ⭐⭐ | Same | Interview questions restricting % and & |
Best Practices
-
Prefer the modulus operator because it is simple and highly readable.
-
Validate user input when using
Scanner. -
Close the
Scannerobject after use. -
Use integer types (
int,long) for even/odd checks. -
Avoid unnecessary nested conditions for such a simple problem.
Common Mistakes
Using Assignment Instead of Comparison
Incorrect:
if (num % 2 = 0)
Correct:
if (num % 2 == 0)
Assuming Negative Numbers Don't Work
Incorrect assumption.
Examples:
-8 % 2 = 0
Even
-7 % 2 = -1
Odd
The logic works correctly for negative integers.
Using Floating-Point Numbers
Even and odd are properties of integers.
Avoid checking values such as:
4.5
Forgetting to Close Scanner
Always call:
scanner.close();
after reading input.
Expert Tips
-
The modulus operator is the standard solution and should be your default choice.
-
The bitwise approach is a common interview follow-up question.
-
All methods execute in constant time (O(1)).
-
Understanding binary representation helps explain why the bitwise method works.
-
Interviewers often ask about edge cases such as negative numbers and zero.
Pros and Cons
| Method | Advantages | Disadvantages |
|---|---|---|
| Modulus | Easy to read, widely used | None for normal applications |
| Bitwise | Very fast, demonstrates binary knowledge | Less intuitive for beginners |
| Ternary | Compact and concise | Can reduce readability if overused |
| Division | Works without % or & |
Less readable than other methods |
Frequently Asked Questions
What is the easiest way to check whether a number is even or odd?
Use the modulus operator:
num % 2 == 0
Does the modulus operator work for negative numbers?
Yes.
Java correctly identifies negative even and odd numbers.
How can I check even or odd without using %?
Use the bitwise operator:
(num & 1) == 0
Which is faster: modulus or bitwise?
Bitwise operations are slightly faster at the hardware level.
However, for most Java applications, the performance difference is negligible.
Can I use the ternary operator?
Yes.
It produces the same result while making the code shorter.
Is zero even?
Yes.
0 % 2 = 0
Therefore, zero is an even number.
Can I check multiple numbers in a loop?
Yes.
Simply place the condition inside a for or while loop.
What is the time complexity?
All approaches perform a constant number of operations.
Time Complexity:
O(1)
Why do interviewers ask this question?
Although simple, this problem tests your understanding of operators, conditional statements, binary representation, edge cases, and alternative problem-solving techniques.
Can Java Streams be used?
Yes.
For example, to filter even numbers from a list:
list.stream()
.filter(n -> n % 2 == 0)
.toList();
However, for checking a single number, a simple if statement is much more appropriate.