Java Program to Check Even or Odd Numbers

by | Jan 1, 2023 | Java, Java Programs

Introduction

This article is based on the several ways in which you can detect whether an integer is odd or even in Java.

Even Numbers

Even Numbers are those numbers that return zero as the remainder when they are divided by 2.  Therefore, we can conclude that all numbers that end with 0, 2, 4, 6, and 8 are surely divisible by 2 and are hence called Even numbers.

Odd Numbers

When a particular number is not divisible by 2 and leaves a remainder of 1, then the number is known as an odd number. Therefore, we can conclude that all numbers that end with 1, 3, 5, 7, and 9 are odd numbers.

Note: If all integers are a universal set and ‘Even’ is a set of all the even numbers in the universal set, Odd can be thought of as intuitively subtracting the Even set from the universal set.

Generic Illustration Using Examples

Input: 13
Output: ODD

Input: 24
Output: EVEN

Java Program to Check Even or Odd Numbers

There are several ways to check whether a number is odd or even, and the methodologies are listed below:

  • Using Brute Force – Naive Approach
  • Using Bitwise OR
  • Using Bitwise AND
  • Using Bitwise XOR

Using BruteForce – Naïve Approach

This is the simplistic version of checking whether a number is odd or even. It is done by dividing the number by 2 and checking for the remainder. If the remainder is 0, it is an even number, while the rest are odd numbers.

import java.io.*;
import java.util.Scanner;
class Demo
public static void main (String args [])
{
int num = 10;
if (num % 2 == 0)
{
System.out.println (“Entered Number if Even”);
}
else
{
System.out.println (“Entered number is odd”);
}
}
}

 

OUTPUT
Entered Number is Even

 

Bitwise Operators

Bitwise Operators are currently an efficient way to determine whether a number is odd or even. The three Bitwise Operators that can be used in this process are Bitwise OR, Bitwise AND, and Bitwise XOR.

Using Bitwise OR

Bitwise OR operations of an even number will get the number incremented by 1.  In the case of odd, it remains unchanged.

Illustration of the Bitwise OR:

Case1:

Number = 12 = 1100 – Representation of 12 in binary
Bitwise OR = 0001 – Representation of 1 in binary
Bitwise sum = 1101 – Representation of 13 in binary

Result – The number was even close, so bitwise OR by 1 increment the value of 1.

Case2:

Number = 15 = 1111 – Representation of 15 in binary
Bitwise OR = 0001 – Representation of 1 in binary
Bitwise sum = 1111 – Representation of 15 in binary

Result – The number was even, so ‘bitwise OR’ by 1 increment the value of 1.

Sample Code Using Bitwise OR

import java.util.*;
public class Demo
{
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”);
}
}
}

 

OUTPUT

Number is Even

Using Bitwise AND

Bitwise AND operation of an odd number will always be equal to 1 because the last bit will be already set. In case of an Even number, it will give 0 as output.

Case1:

Number = 5 = 0101 – Representation of 5 in binary
Bitwise OR = 0001 – Representation of 1 in binary
Bitwise sum = 101 – Representation of Sum in binary

Result – The number was odd, so bitwise AND by 1 is 1.

Case2:

Number = 8 = 1000 – Representation of 8 in binary
Bitwise OR = 0001 – Representation of 1 in binary
Bitwise sum = 0000 – Representation of 0 in binary

Result – The number was even, so ‘bitwise AND’ by 1 sets the last digit as 0.

Sample Code

import java.util.*;
public class Demo
{
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”);
}
}
}

 

OUTPUT

Number is Odd

Using Bitwise XOR

Bitwise XOR operation of an even number gets the number incremented by  1. In the case of an odd number, it gets the number decremented by 1.

It is the recommended approach.

The illustration follows the pattern below:

Case1:

Number = 5 = 0101 – Representation of 5 in binary
Bitwise XOR = 0001 – Representation of 1 in binary
Bitwise sum = 1101 – Representation of 4 in binary

Result – The number was odd, so ‘bitwise AND’ by 1 decrement the value of 1.

Case2:

Number = 8 = 1000 – Representation of 8 in binary
Bitwise XOR = 0001 – Representation of 1 in binary
Bitwise sum = 1101 – Representation of 13 in binary

Result – Number was even so ‘bitwise AND’ by 1 increment the value of 1.

Sample Code

import java.util.*;
public class Demo
{
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”);
}
}
}

 

OUTPUT

Number is Odd

 

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.