00:00

Java Program to Check Whether a Number is Even or Odd

Last updated: 3 min read

An even number divides by 2 with no remainder (0, 2, 4, 6, ...). An odd number leaves a remainder of 1 (1, 3, 5, 7, ...). In Java the remainder operator % gives this directly: number % 2 == 0 means the number is even.

This page shows four ways to check whether a number is even or odd in Java, from a simple if-else to the bitwise AND operator.

1. Even or Odd Using if-else

The program checks several numbers in a loop. For each one, number % 2 is 0 for even numbers and 1 for odd numbers.

EvenOdd.java
public class EvenOdd {
    public static void main(String[] args) {
        int[] numbers = {8, 15, 24, 31, 0};

        for (int number : numbers) {
            if (number % 2 == 0) {
                System.out.println(number + " is an even number.");
            } else {
                System.out.println(number + " is an odd number.");
            }
        }
    }
}
Output
8 is an even number.
15 is an odd number.
24 is an even number.
31 is an odd number.
0 is an even number.

2. Even or Odd with User Input

This version reads the number from the keyboard with the Scanner class.

EvenOddInput.java
import java.util.Scanner;

public class EvenOddInput {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int number = scanner.nextInt();
        scanner.close();

        if (number % 2 == 0) {
            System.out.println(number + " is an even number.");
        } else {
            System.out.println(number + " is an odd number.");
        }
    }
}
Output 1
Enter a number: 44
44 is an even number.
Output 2
Enter a number: 77
77 is an odd number.

3. Even or Odd Using the Ternary Operator

The ternary operator condition ? valueIfTrue : valueIfFalse replaces the whole if-else with one expression.

EvenOddTernary.java
import java.util.Scanner;

public class EvenOddTernary {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int number = scanner.nextInt();
        scanner.close();

        String result = (number % 2 == 0) ? "even" : "odd";
        System.out.println(number + " is an " + result + " number.");
    }
}
Output (with sample input)
Enter a number: -7
-7 is an odd number.
Note: this also works for negative numbers. For an odd negative number, number % 2 is -1, not 1, so always compare with 0 rather than writing number % 2 == 1.

4. Even or Odd Using the Bitwise AND Operator

In binary, the last bit of every odd number is 1 and the last bit of every even number is 0. number & 1 keeps only that last bit, so it is 0 for even numbers and 1 for odd numbers.

EvenOddBitwise.java
public class EvenOddBitwise {
    public static void main(String[] args) {
        int[] numbers = {10, 13, -6, -9};

        for (int number : numbers) {
            boolean isEven = (number & 1) == 0;
            System.out.println(number + (isEven ? " is even" : " is odd"));
        }
    }
}
Output
10 is even
13 is odd
-6 is even
-9 is odd

5. Summary

  • A number is even when number % 2 == 0; otherwise it is odd.
  • Compare the remainder with 0 so that negative odd numbers are handled correctly.
  • The ternary operator gives a one-line version of the same check.
  • (number & 1) == 0 checks the last bit and is another correct way to test for even numbers.

Related Programs