Open In App

Java Program to Check if a Given Integer is Odd or Even

Last Updated : 22 Jun, 2022
Comments
Improve
Suggest changes
27 Likes
Like
Report

A number that is divisible by 2 and generates a remainder of 0 is called an even number. All the numbers ending with 0, 2, 4, 6, and 8 are even numbers. On the other hand, number that is not divisible by 2 and generates a remainder of 1 is called an odd number. All the numbers ending with 1, 3, 5,7, and 9 are odd numbers. Do refer to the below illustration to get what is supposed to be conveyed out basics here via generic Illustration for any random integer, check whether it is even or odd. 

Input : 13
Output: ODD
Input : 24
Output: EVEN

Methods:

There are various ways to check whether the given number is odd or even. Some of them are as follows starting from the brute force approach ending up at the most optimal approach.

  1. Using Brute Force- Naive Approach
  2. Using bitwise operators
    • Using Bitwise OR
    • Using Bitwise AND
    • Using Bitwise XOR
  3. By Checking the Least Significant Bit

Method 1: Brute Force Naive Approach

It is to check the remainder after dividing by 2. Numbers that are divisible by 2 are even else odd.

Example


Output
Entered Number is Even

Time Complexity: O(1)

Auxiliary Space: O(1)

Now let us dwell on optimal approaches as follows below as follows with help of bitwise operators

Method 2: Using bitwise operators

  • Bitwise OR
  • Bitwise AND or Bitwise XOR

2-A: Using Bitwise OR

Bitwise OR operation of the even number by 1 increment the value of the number by 1 otherwise it remains unchanged.

Illustration: Bitwise OR 

    Number = 12              1  1  0  0    - Representation of 12 in Binary Format
                Bitwise OR   0  0  0  1    - Representation of  1 in Binary Format
                            
                             1  1  0  1    - Representation of 13 in Binary Format
    Result- Number was even so bitwise Or by 1 increment the value by 1
    Number = 15            1  1  1  1    - Representation of 15 in Binary Format
               Bitwise OR  0  0  0  1    - Representation of  1 in Binary Format
                        
                           1  1  1  1    - Representation of 15 in Binary Format
    Result- Number was odd so bitwise Or by 1 doesn't increment the value by 1

Example


Output
Number is Even

Time Complexity: O(1)

Auxiliary Space: O(1)

2-B: Using Bitwise AND

Bitwise AND operation of the odd number by 1 will be 1 because the last bit will be already set otherwise it will give 0. 

Illustration: Bitwise AND 

    Number = 5              0  1  0  1    - Representation of  5 in Binary Format
               Bitwise AND  0  0  0  1    - Representation of  1 in Binary Format
                          
                            0  0  0  1    - Representation of  1 in Binary Format
    Result- Number was odd so bitwise And by 1 is 1
        Number = 8            1  0  0  0    - Representation of  8 in Binary Format
             Bitwise AND  0  0  0  1    - Representation of  1 in Binary Format
                       
                          0  0  0  0    - Representation of  0 in Binary Format
    Result- Number was even so bitwise And by 1 is 0

Example


Output
Number is Odd

Time Complexity: O(1)

Auxiliary Space: O(1)

 2-C: Using Bitwise XOR

Bitwise XOR operation of the even number by 1 increment the value of the number by 1 otherwise it decrements the value of the number by 1 if the value is odd. It is the most optimal approach.

Illustration: Bitwise XOR  

    Number = 5              0  1  0  1    - Representation of  5 in Binary Format
               Bitwise XOR  0  0  0  1    - Representation of  1 in Binary Format
                       
                            0  1  0  0    - Representation of  4 in Binary Format
    Result- Number was odd so bitwise And by 1 decrement the value
    Number = 8            1  0  0  0    - Representation of  8 in Binary Format
             Bitwise XOR  0  0  0  1    - Representation of  1 in Binary Format

                          1  0  0  1    - Representation of  9 in Binary Format
    Result- Number was even so bitwise And by 1 increment the value

Example


Output
Number is Odd

Time Complexity: O(1)

Auxiliary Space: O(1)

 Method 3: Checking the LSB of the Number

The LSB(Least Significant Bit) of an even number is always 0 and that of an odd number is always 1.

Example 


Output
0 : Zero
1 : Odd
2 : Even
3 : Odd
4 : Even
5 : Odd
6 : Even
7 : Odd
8 : Even
9 : Odd
10 : Even

Time Complexity: O(1)

Auxiliary Space: O(1)


Java Program to Check Whether Number is Even or Odd
Article Tags :
Practice Tags :

Similar Reads