Check Even or Odd Number in Java

Java

Number can be categorized into two groups: even or odd. It is an even number if the number is evenly divisible by 2. It is an odd number if the number is not evenly divisible by 2.

In this tutorial, we’ll show you some examples of how to check and identify the even or odd number in Java.

Note:

  • Even number: A number is evenly divisible by 2.
  • Odd number: A number is not evenly divisible by 2.

What You’ll Need

Example 1: Use Modulus Operator

In this example, we will use modulus operator % to check the even or odd number.

int[] intArrays = {-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5};
for (int number: intArrays) {
    if (number % 2 == 0) {
        // This is even number
        System.out.println(number + " is even number");
    } else {
        // This is odd number
        System.out.println(number + " is odd number");
    }
}

Example 2: Use Bitwise AND Operator

In this example, we will use bitwise AND operator & to check the even or odd number.

Bitwise AND operation rules:

  • 0 & 0 = 0
  • 0 & 1 = 0
  • 1 & 0 = 0
  • 1 & 1 = 1
int[] intArrays = {-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5};
for (int number: intArrays) {
    if ((number & 1) == 0) {
        // This is even number
        System.out.println(number + " is even number");
    } else {
        // This is odd number
        System.out.println(number + " is odd number");
    }
}

Java Code

CheckEvenOddNumberExample.java

package com.chankok.operators;

public class CheckEvenOddNumberExample {

    public static void main(String[] args) {

        // Initial the integer array
        int[] intArrays = {-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5};

        /*
         * Example 1: Check even or odd number using modulus operator (%)
         */
        System.out.println("Example 1: Use modulus operator");
        for (int number: intArrays) {
            if (number % 2 == 0) {
                // This is even number
                System.out.println(number + " is even number");
            } else {
                // This is odd number
                System.out.println(number + " is odd number");
            }
        }

        // Print new line
        System.out.println();

        /*
         * Example 2: Check even or odd number using bitwise AND operator (&)
         */
        System.out.println("Example 2: Use bitwise AND operator");
        for (int number: intArrays) {
            if ((number & 1) == 0) {
                // This is even number
                System.out.println(number + " is even number");
            } else {
                // This is odd number
                System.out.println(number + " is odd number");
            }
        }

    }

}

Output

Example 1: Use modulus operator
-5 is odd number
-4 is even number
-3 is odd number
-2 is even number
-1 is odd number
0 is even number
1 is odd number
2 is even number
3 is odd number
4 is even number
5 is odd number

Example 2: Use bitwise AND operator
-5 is odd number
-4 is even number
-3 is odd number
-2 is even number
-1 is odd number
0 is even number
1 is odd number
2 is even number
3 is odd number
4 is even number
5 is odd number