**Even Number: **A number which 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.

**Odd Number: **An 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.

Generic Illustration for any random integer, check whether it is even or odd

Input :13Output:ODDInput :24Output:EVEN

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 OR****Using****Bitwise AND****Using Bitwise XOR**

**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.

Naive Approach is implemented as below:

## Java

`// Java Program to Check if ` `// a Given Integer is Odd or Even ` ` ` `// Importing Classes/Files ` `import` `java.io.*; ` `import` `java.util.Scanner; ` ` ` `class` `GFG { ` ` ` ` ` `// Main Driver Method ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `// Declare the integer variable ` ` ` `int` `num = ` `10` `; ` ` ` ` ` `// If condition to check if the remainder is zero ` ` ` `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"` `); ` ` ` `} ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Entered Number is Even

**A Better Approach is to use Bitwise Operators**

- Bitwise OR
- Bitwise AND or Bitwise XOR

**2. 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 for Bitwise OR followed by implementation:

Case 1: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 1Case 2: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

## Java

`// Java Program to Check if ` `// a Given Integer is Odd or Even ` ` ` `// Importing Classes/Files ` `import` `java.util.*; ` ` ` `public` `class` `GFG { ` ` ` `// Driver Main code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `// 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) { ` ` ` `System.out.println(` `"Number is Even"` `); ` ` ` `} ` ` ` `else` `{ ` ` ` `System.out.println(` `"Number is Odd"` `); ` ` ` `} ` ` ` `} ` `}` |

*chevron_right*

*filter_none*

**Output**

Number is Even

**3. 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 for Bitwise AND followed by implementation:

Case 1: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 Case 2: 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

## Java

`// Java Program to Check if ` `// a Given Integer is Odd or Even ` ` ` `// Importing Classes/Files ` `import` `java.util.*; ` ` ` `public` `class` `GFG { ` ` ` ` ` `// Driver Main Method ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `// Declare variable ` ` ` `int` `n = ` `91` `; ` ` ` ` ` `// Condition Check ` ` ` `// Bitwise AND of any odd number by 1 gives 1 ` ` ` `if` `((n & ` `1` `) == ` `1` `) { ` ` ` `System.out.println(` `"Number is Odd"` `); ` ` ` `} ` ` ` `else` `{ ` ` ` `System.out.println(` `"Number is Even"` `); ` ` ` `} ` ` ` `} ` `}` |

*chevron_right*

*filter_none*

**Output**

Number is Odd

**4. 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 for Bitwise XOR followed by implementation:

Case 1: 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 Case 2: 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

## Java

`// Java Program to Check if ` `// a Given Integer is Odd or Even ` ` ` `// Importing Classes/Files ` `import` `java.util.*; ` `public` `class` `GFG { ` ` ` ` ` `// Driver Main Method ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `// Declare Variable ` ` ` `int` `num = ` `99` `; ` ` ` ` ` `// Condition Check ` ` ` `// if number^1 increments by 1 then its even number, ` ` ` `// else odd ` ` ` `if` `((num ^ ` `1` `) == num + ` `1` `) { ` ` ` `System.out.println(` `"Number is Even"` `); ` ` ` `} ` ` ` `else` `{ ` ` ` `System.out.println(` `"Number is Odd"` `); ` ` ` `} ` ` ` `} ` `}` |

*chevron_right*

*filter_none*

**Output**

Number is Odd

Attention reader! Don’t stop learning now. Get hold of all the important **Java Foundation** and Collections concepts with the **Fundamentals of Java and Java Collections Course** at a student-friendly price and become industry ready.