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.
- Using Brute Force- Naive Approach
- Using bitwise operators
- Using Bitwise OR
- Using Bitwise AND
- Using Bitwise XOR
- 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
import java.io.*;
import java.util.Scanner;
class GFG {
public static void main(String[] args)
{
int num = 10 ;
if (num % 2 == 0 ) {
System.out.println( "Entered Number is Even" );
}
else {
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
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int n = 100 ;
if ((n | 1 ) > n) {
System.out.println( "Number is Even" );
}
else {
System.out.println( "Number is Odd" );
}
}
}
|
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
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int n = 91 ;
if ((n & 1 ) == 1 ) {
System.out.println( "Number is Odd" );
}
else {
System.out.println( "Number is Even" );
}
}
}
|
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
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int num = 99 ;
if ((num ^ 1 ) == num + 1 ) {
System.out.println( "Number is Even" );
}
else {
System.out.println( "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
import java.util.*;
public class GFG {
public static String testOddEvenByCheckingLSB( int a)
{
if (a != 0 ) {
if (Integer.toBinaryString(a).endsWith( "0" )) {
return "Even" ;
}
else {
return "Odd" ;
}
}
else {
return "Zero" ;
}
}
public static void main(String[] args)
{
for ( int i = 0 ; i <= 10 ; i++) {
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)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
22 Jun, 2022
Like Article
Save Article