7 different Java programs to check if a number is Even or odd

Java program to check if a number is Even or odd:

In this post, we will learn different ways to check if a number is Even or Odd in Java.

We will use if else statement to check if a user input number is even or odd and print one message based on that.

Even and odd numbers:

A number is called Even if it is perfectly divisible by 2, i.e. if we divide the number by 2 and if the remainder is 0 then it is called an even number.

Similarly, if a number is not perfectly divisible by 2, it is called an odd number.

For example, 4, 8, 10 etc. are even numbers and 5, 7, 9 etc. are odd numbers.

Method 1: Check if a number is even or odd by using modulo operator:

We can use the modulo or remainder operator get the remainder. This operator is % and if we use num % 2, it will return us the remainder value for num/2. So, if num % 2 is equal to 0, we can call that it is an even number. Else, it is an odd number.

Let’s write down the complete program:

import java.util.Scanner;

class Main {

    public static void main(String[] args) {
        int no;
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter a number: ");
        no = sc.nextInt();

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

In this program,

  • no is an integer variable to hold the user input value.
  • sc is a Scanner object to read user inputs.
  • Ask the user to enter a number. Read that number using the scanner variable and store it in no.
  • The if block is checking if the input number is even or not, i.e. if the return value of no % 2 is even or not.
    • If it is even, it will move inside the if block. Else, it will move inside the else block.

If you run this program, it will print output as like below:

Enter a number: 
123
It is an odd number

Enter a number: 
120
It is an even number

Method 2: Check if a number is even or odd by using modulo operator and ternary operator:

We can use ternary operator instead of an if-else block. It is easy to use and concise. The ternary operator is defined as like below:

condition ? exp1 : exp2

condition is a logical condition that returns a boolean value. If it is true, it executes the expression exp1. Else, i.e. if the condition returns false, it executes exp2.

Let’s use it to print if a number is odd or even:

import java.util.Scanner;

class Main {

    public static void main(String[] args) {
        int no;
        String result;
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter a number: ");
        no = sc.nextInt();

        result = (no % 2 == 0) ? "It is an even number" : "It is an odd number";

        System.out.println(result);
    }
}

It will print similar output.

We are storing the final string in the result variable and printing its value.

Method 3: Using bitwise AND:

For an odd number, the last bit is set, i.e. it is 1. For even numbers, the last bit is 0. If we use AND operator with 1, it will always be 1 for a odd number.

So,

import java.util.Scanner;

class Main {

    public static void main(String[] args) {
        int no;
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter a number: ");
        no = sc.nextInt();

        if ((no & 1) != 1) {
            System.out.println("It is an even number");
        } else {
            System.out.println("It is an odd number");
        }
    }
}

Method 4: Using bitwise OR:

If we find the OR with 1, then the value will be greater than the number if it is an even number. Since the last bit is set for an even number, OR with 1 will always more than the number.

import java.util.Scanner;

class Main {

    public static void main(String[] args) {
        int no;
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter a number: ");
        no = sc.nextInt();

        if ((no | 1) > no) {
            System.out.println("It is an even number");
        } else {
            System.out.println("It is an odd number");
        }
    }
}

Method 5: Using bitwise XOR:

If we do bitwise XOR with a number no with 1, it will be no + 1 for an even number. For an odd number, it will not be equal to no + 1.

import java.util.Scanner;

class Main {

    public static void main(String[] args) {
        int no;
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter a number: ");
        no = sc.nextInt();

        if ((no ^ 1) == no + 1) {
            System.out.println("It is an even number");
        } else {
            System.out.println("It is an odd number");
        }
    }
}

Method 6: Using shift operators:

We can also use the shift operators to check if a number is even or odd:

import java.util.Scanner;

class Main {

    public static void main(String[] args) {
        int no;
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter a number: ");
        no = sc.nextInt();

        if ((no >> 1) << 1 == no) {
            System.out.println("It is an even number");
        } else {
            System.out.println("It is an odd number");
        }
    }
}

Method 7: By using the division operator:

We can also use the division operator to check if a number is odd or even. If we divide the number by 2 and multiply it by 2, it will be always equal to the number for even numbers. It will not be equal to the number for odd numbers.

For example,

import java.util.Scanner;

class Main {

    public static void main(String[] args) {
        int no;
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter a number: ");
        no = sc.nextInt();

        if ((no / 2) * 2 == no) {
            System.out.println("It is an even number");
        } else {
            System.out.println("It is an odd number");
        }
    }
}

java even odd check

You might also like: