Check Whether a Number is Even or Odd in Java



In this article, we will learn to check whether a number is even or odd using Java. If a number is divisible by 2, then it is an even number otherwise it is odd. Therefore, we can verify a given number is even or odd by dividing it by 2. We will be using 2 different methods one with the modulus operator (%) and another with the bitwise AND operator (&).

Problem Statement

We are given an integer number as an input and our task is to write a Java program to check whether that number is even or odd.

Input 1

number = 45

Output 1

result = odd

Input 2

number = 42;

Output 2

result = even

Different Approaches

Following are the different approaches to check whether a number is even or odd ?

Using Modulus Operator

The modulus operator (%) is used to find the remainder of a division operation between two numbers. To check whether the given number is even or odd, perform the modulo operation between 2 and given number. If it returns 0, then it is even, otherwise it is odd.

  • Declare and initialize the integer variable my_input to store the number.
  • Print the number to the console.
  • Use the modulus operator (% 2) to check if the number is even or odd.
  • Print the result based on the condition.

Example

In the following example we are verifying whether the given number is even or odd using the java Modulus Operator ?

public class EvenOdd {
   public static void main(String[] args) {
      int my_input;
      my_input = 45;
      System.out.println("The number is defined as " +my_input);
      if(my_input % 2 == 0)
         System.out.println("The number " +my_input + " is an even number");
      else
         System.out.println("The number " +my_input + " is an odd number");
   }
}

Output

The number is defined as 45
The number 45 is an odd number

Using Bitwise AND Operator

In Java, the binary representation of even numbers ends with 0, and odd numbers end with 1. When we perform a bitwise AND operation between a given number and 1, we can determine whether it is even or odd.

  • Declare and initialize the integer variable my_input to store the number.
  • Print the number to the console.
  • Perform a bitwise AND operation (my_input & 1) to check if the number is even or odd.
  • Print the result based on the condition.

Example

Now, let us determine whether a given number is even or odd using the bitwise operator ?

public class EvenOdd {
   public static void main(String[] args) {
      int my_input;
      my_input = 42;
      System.out.println("The number is defined as " +my_input);
      if ((my_input & 1) == 0)
         System.out.println("The number " + my_input + " is an even number");
      else
         System.out.println("The number " + my_input + " is an odd number");
   }
}

Output

The number is defined as 42
The number 42 is an even number
Updated on: 2024-09-24T21:44:32+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements