Java Program to Check Even or Odd Number
In Java, you can check if a number is even or odd by using the modulus operator
% like this: if (number % 2 == 0) { System.out.println("Even"); } else { System.out.println("Odd"); }.Examples
Input4
Output4 is Even
Input7
Output7 is Odd
Input0
Output0 is Even
How to Think About It
To check if a number is even or odd, divide it by 2 and look at the remainder. If the remainder is 0, the number is even because it divides evenly by 2. If the remainder is not 0, the number is odd because it leaves a remainder when divided by 2.
Algorithm
1
Get the input number from the user.2
Calculate the remainder when the number is divided by 2 using the modulus operator.3
If the remainder is 0, print that the number is even.4
Otherwise, print that the number is odd.Code
java
import java.util.Scanner; public class EvenOddChecker { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int number = scanner.nextInt(); if (number % 2 == 0) { System.out.println(number + " is Even"); } else { System.out.println(number + " is Odd"); } scanner.close(); } }
Output
Enter a number: 7
7 is Odd
Dry Run
Let's trace the input 7 through the code
1
Input number
User enters 7, so number = 7
2
Calculate remainder
7 % 2 = 1 (since 7 divided by 2 leaves remainder 1)
3
Check remainder
Remainder is 1, which is not 0, so number is odd
4
Print result
Prints '7 is Odd'
| Step | Operation | Value |
|---|---|---|
| 1 | Input number | 7 |
| 2 | 7 % 2 | 1 |
| 3 | Check if remainder == 0 | False |
| 4 | Print output | 7 is Odd |
Why This Works
Step 1: Using modulus operator
The % operator gives the remainder of division, which helps us know if a number divides evenly by 2.
Step 2: Even number condition
If the remainder is 0, it means the number is divisible by 2 and is even.
Step 3: Odd number condition
If the remainder is not 0, the number is not divisible by 2 and is odd.
Alternative Approaches
Using bitwise AND operator
java
import java.util.Scanner; public class EvenOddChecker { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int number = scanner.nextInt(); if ((number & 1) == 0) { System.out.println(number + " is Even"); } else { System.out.println(number + " is Odd"); } scanner.close(); } }
This method uses bitwise AND with 1 to check the last bit; it's often faster but less intuitive for beginners.
Using ternary operator
java
import java.util.Scanner; public class EvenOddChecker { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int number = scanner.nextInt(); String result = (number % 2 == 0) ? "Even" : "Odd"; System.out.println(number + " is " + result); scanner.close(); } }
This approach uses a compact ternary operator for cleaner code but does the same check.
Complexity: O(1) time, O(1) space
Time Complexity
The check uses a single modulus operation which takes constant time regardless of input size.
Space Complexity
Only a few variables are used, so the space needed is constant.
Which Approach is Fastest?
Both modulus and bitwise AND methods run in constant time; bitwise AND can be slightly faster but is less readable.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Modulus operator (%) | O(1) | O(1) | Readability and simplicity |
| Bitwise AND (&) | O(1) | O(1) | Performance in low-level or large-scale systems |
| Ternary operator | O(1) | O(1) | Compact code style |
Use
number % 2 == 0 to quickly check if a number is even in Java.Beginners often forget to use the modulus operator and try to check evenness by dividing and comparing the result directly.