Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaJava Program to Check if a Given Integer is Odd or Even

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

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

Java




// Java Program to Check if Given Integer is Odd or Even
// Using Brute Forcew Approach
 
// Importing required classes
import java.io.*;
import java.util.Scanner;
 
// Main class
class GFG {
 
    // Main Driver Method
    public static void main(String[] args)
    {
        // Declaring and initializing integer variable
        int num = 10;
 
        // Checking if number is even or odd number
        // via remainder
        if (num % 2 == 0) {
 
            // If remainder is zero then this number is even
            System.out.println("Entered Number is Even");
        }
 
        else {
 
            // If remainder is not zero then this number is
            // odd
            System.out.println("Entered Number is Odd");
        }
    }
}


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

Java




// Java Program to Check if Given Integer is Odd or Even
// Using Bitwise OR
 
// Importing required classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring and initializing integer variable
        // to be checked
        int n = 100;
 
        // Condition check
        // if n|1 if greater than n then this number is even
        if ((n | 1) > n) {
 
            // Print statement
            System.out.println("Number is Even");
        }
        else {
 
            // Print statement
            System.out.println("Number is Odd");
        }
    }
}


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

Java




// Java Program to Check if Given Integer is Odd or Even
// Using Bitwise AND
 
// Importing required classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Declare and initializing integer variable
        int n = 91;
 
        // Condition Check
        // Bitwise AND of any odd number by 1 gives 1
        if ((n & 1) == 1) {
 
            // Print statement
            System.out.println("Number is Odd");
        }
        else {
 
            // Print statement
            System.out.println("Number is Even");
        }
    }
}


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

Java




// Java Program to Check if Given Integer is Odd or Even
// Using Bitwise XOR
 
// Importing required classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Declare and initializing integer variable
        int num = 99;
 
        // Condition Check
        // if number^1 increments by 1 then its even number,
        // else odd
        if ((num ^ 1) == num + 1) {
 
            // Print statement
            System.out.println("Number is Even");
        }
        else {
 
            // Print statement
            System.out.println("Number is Odd");
        }
    }
}


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 

Java




// Java Program to Check if Given Integer is Odd or Even
// by checking the LSB of the Number
 
// Importing required classes
import java.util.*;
 
// Main class
// TestEvenOddByCheckingLSB
public class GFG {
 
    // Method 1
    // To test number is even or odd
    public static String testOddEvenByCheckingLSB(int a)
    {
 
        if (a != 0) {
            if (Integer.toBinaryString(a).endsWith("0")) {
                return "Even";
            }
            else {
                return "Odd";
            }
        }
 
        // Here we will land if
        // it does not ends with 0
        else {
            return "Zero";
        }
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
 
        // Iterationg over using for loop
        for (int i = 0; i <= 10; i++) {
 
            // Calling the function and printing
            // corresponding number is even or odd
            System.out.println(
                i + " : " + testOddEvenByCheckingLSB(i));
        }
    }
}


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)

RELATED ARTICLES

Most Popular

Recent Comments