Java Program – Check if Number is Even or Odd

An integer is even if it is exactly divisible by 2. Otherwise, it is odd. In Java, we can test this using the remainder operator %.

For an integer number, the expression number % 2 gives the remainder after division by 2. If the remainder is 0, the number is even. If the remainder is not 0, the number is odd.

</>
Copy
number % 2 == 0

The condition above evaluates to true for even integers such as 2, 8, 0, and -4. Therefore, 2 is an even number, and 0 is also even.

How the Java % Operator Identifies Even and Odd Numbers

The % operator returns the remainder of an integer division. Consider these examples:

10 % 2 = 0
7 % 2 = 1
0 % 2 = 0
-8 % 2 = 0
-7 % 2 = -1

An even integer always produces a remainder of 0 when divided by 2. For an odd integer, the remainder is nonzero. Notice that a negative odd integer can produce -1 in Java. This matters when writing a condition intended to work with all integers.

Example 1 – Check if Number is Even or Odd Using if-else

In the following Java program, we shall read a number from console entered by user in standard input. Then we shall check if this number is even or odd using Java If-Else statement.

If a stores the integer, a%2 gives the remainder when a is divided by 2. The condition a%2==0 checks whether that remainder is zero. If the condition is true, a is even. Otherwise, a is odd.

Example.java

</>
Copy
import java.util.Scanner;

/**
 * Java Program - Check if Number is Even or Odd
 */

public class Example {

	public static void main(String[] args) {
		//create a scanner to read bytes from console entered by user via keyboard
		Scanner scanner = new Scanner(System.in);
		
		System.out.print("Enter a number : ");
		//read integer from user
		int a = scanner.nextInt();
		
		//check if the number is even or odd
		if(a%2==0) {
			System.out.println("The number is even.");
		} else {
			System.out.println("The number is odd.");
		}
		
		//close the scanner
		scanner.close();
	}
}

Run the above program and enter an integer. For example, if you enter 24, 24 % 2 is 0, so the program prints that the number is even. If you enter 89, the remainder is nonzero, so the program prints that the number is odd.

Java Program - Check if Number is Even or Odd
Java Program - Check if Number is Even or Odd

Example 2 – Check if Number is Odd Using Remainder 1

The following program checks whether the remainder is 1. This works for non-negative integers: an odd non-negative integer divided by 2 has a remainder of 1.

Example.java

</>
Copy
import java.util.Scanner;

/**
 * Java Program - Check if Number is Even or Odd
 */

public class Example {

	public static void main(String[] args) {
		//create a scanner to read bytes from console entered by user via keyboard
		Scanner scanner = new Scanner(System.in);
		
		System.out.print("Enter a number : ");
		//read integer from user
		int a = scanner.nextInt();
		
		//check if the number is even or odd
		if(a%2==1) {
			System.out.println("The number is odd.");
		} else {
			System.out.println("The number is even.");
		}
		
		//close the scanner
		scanner.close();
	}
}

When you run the program, it prompts you to enter a number. For a positive odd number such as 89, 89 % 2 is 1, so the first branch executes.

Output

Enter a number : 89
The number is odd.

Check Negative Odd Numbers Correctly in Java

For code that must handle both positive and negative integers, avoid using only number % 2 == 1 as the odd-number test. Java keeps the sign of the dividend in the remainder, so an expression such as -7 % 2 evaluates to -1, not 1.

A reliable way to identify an odd integer is to test whether the remainder is not zero:

</>
Copy
number % 2 != 0

The following program therefore works for positive integers, negative integers, and zero.

</>
Copy
import java.util.Scanner;

public class Example {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter an integer : ");
        int number = scanner.nextInt();

        if (number % 2 != 0) {
            System.out.println("The number is odd.");
        } else {
            System.out.println("The number is even.");
        }

        scanner.close();
    }
}

For example, entering -7 produces the following result:

Enter an integer : -7
The number is odd.

Check Even or Odd Without User Input

If the value is already available in the program, a Scanner is not required. Assign the integer to a variable and apply the same remainder test.

</>
Copy
public class Example {
    public static void main(String[] args) {
        int number = 42;

        if (number % 2 == 0) {
            System.out.println(number + " is even.");
        } else {
            System.out.println(number + " is odd.");
        }
    }
}
42 is even.

Even-or-Odd Check Using the Java Ternary Operator

When only a short value or message has to be selected, the same condition can be written with Java’s conditional, or ternary, operator. The test itself remains number % 2 == 0.

</>
Copy
public class Example {
    public static void main(String[] args) {
        int number = 17;

        String result = (number % 2 == 0) ? "even" : "odd";
        System.out.println(number + " is " + result + ".");
    }
}
17 is odd.

Java Even-or-Odd Conditions to Remember

  • Use number % 2 == 0 to check whether an integer is even.
  • Use number % 2 != 0 to check whether an integer is odd when negative values are also possible.
  • 0 is even because 0 % 2 is 0.
  • 2 is even because it is exactly divisible by 2.
  • The same remainder test works for positive and negative values stored in Java integer types.

Java Even-or-Odd Program Summary

In this Java Tutorial, we learned how to check whether an integer is even or odd using the remainder operator and an if-else statement. The most direct even-number test is number % 2 == 0. When checking specifically for odd numbers across both positive and negative integers, number % 2 != 0 is the safer condition.