Wednesday, September 20, 2023

How to Check If Number is Even or Odd without using Modulus or Remainder Operator? Example

Write a Java program to find if a number is odd or even is one of the basic programming exercises, and anyone will be happy to see this in actual Java Interviews, wouldn't you? By the way, did I said easy, well there is a little twist there, you need to check odd and even without using modulus (%) or remainder operator in Java. Since many programmers, especially freshers are familiar with % operators, this nice little trick does put them into thinking mode, which is what the interviewer wants. This question is on a similar level of checking if a number is a palindrome or not if you have practiced such questions, it would be easy to find a solution.

Anyway, now, it's your turn to show off how good are you with different operators in Java and how quickly you can think of alternative solutions. Well, there are a couple of ways to check if a number is even or odd without using a modulus operator. 

The first one is, by using the division operator, and the second method is by using bitwise operator in Java. An even number is an integer number is totally divisible by 2 i.e. when you divide even number by 2, the remainder is zero. 

While an odd number is an integer number, which is not multiple of two, if we divide an odd number by 2, the result would be a fraction. If we focus on this definition, we always think about the remainder operator, but we can still use the division operator to check if the number is even and odd, how? let's see.



Even or Odd using division operator

Checking Even or Odd Number in Java without remainder or modulus operator
Division operator in Java returns quotient, if we first divide a number by two and then multiply result which is quotient to two, we will get a number which is always less in case of odd number and always equal in case of even number. By using this property you can check if a number is odd or even :
int quotient = number/2;
if(quotient*2== number){
   System.out.println("Even number");           
}

1. Odd or Even checking using bitwise AND operator

Another way to solve this problem without using modulus operator is, by using bitwise AND operator. Since integer numbers are represented as 2's complement and even number has 0 as there LSB, if we perform a bitwise AND between 1 and number, result will be zero. This would be enough to check if a number is even or odd in Java.

if((number & 1) == 0){
    System.out.println("Even number");
}

Here is a complete Java program to test if number is even or odd without using modulus operators. This program uses both the approach e.g. using division and bitwise operator to identify if a number is odd or even.


/**
 * Java program to check if a number is even or odd without using modulus or remainder
 * operator. This examples uses bitwise AND and division operator to check evenness.
 *
 * @author Javin Paul
 */
public class EvenOrOdd {

    public static void main(String args[]) {
     
        //Testing, let's test both methods for positive and negative integers       
        System.out.println("Checking if a number is even or odd using 
                                 division and bitwise operator");
        for(int i= -1; i<2; i++){
            isEvenOrOdd(i); //calling division operator method
            isOddOrEven(i); //calling
        }       
       
    }   
       
    /*
     * checking even and odd number without using modulus or remainder operator, Instead
     * this method uses division operator.
     */
    public static void isEvenOrOdd(int number){
        int quotient = number/2;
       
        if(quotient*2== number){
            System.out.println("Using division operator: "  + number + " is Even number");
           
        }else{
            System.out.println("Using division operator: "  + number  + " is Odd number");
        }
    }
   
    /*
     * This method uses bitwise AND (&) operator to check if a number is
     * even or odd in Java
     */
    public static void isOddOrEven(int number){
        if((number & 1) == 0){
            System.out.println("Using bitwise operator: "  + number  + " is Even number");
        }else{
            System.out.println("Using bitwise operator: "  + number  + " is Odd number");
        }
    }  
  
}

Output:
Checking if a number is even or odd using division and bitwise operator
Using division operator: -1 is Odd number
Using bitwise operator: -1 is Odd number
Using division operator: 0 is Even number
Using bitwise operator: 0 is Even number
Using division operator: 1 is Odd number
Using bitwise operator: 1 is Odd number

That's all on How to find if an integer is even or odd in Java, without using modulus or remainder operator. The interviewer may twist this question more, by not allowing you to either use a bitwise operator or division operator, that's why it's better to know different alternatives to solve a problem, even if you think that a trivial programming exercise.

And now is the quiz time, What is the time complexity of this solution to check if a given number is even or odd ? And what is difference between a bitwise and bitshift operator in Java?

13 comments :

Unknown said...

C = A – B * (A / B). is equal to C = A % B

manikandan said...

Indefy the odd or even number without using modulus,division and operators

javin paul said...

Hello @manikandan, what is meaning of your comment, sorry can you please elaborate?

manikandan said...

how to find odd or even number but notuseing /,%,<,>,=<,=>

javin paul said...

@manikandan, you can use bitwise AND operator as shown in first example. Even number has their LSB (lowest significant bit) as 0 while Odd number has it 1, you can check that bit to determine if number is even or odd.

manikandan said...

@javin paul i have not understand please write a condition

javin paul said...

Hi @mani, It's already there in the article, second example. nevermind here I post for you again :

if((number & 1) == 0){
System.out.println("Even number");
}

number & 1 == 0 is checking last bit of number because & will return 1 if both operand is 1.

rfilipov said...

Can you think of a solution which doesn't make use of any bitwise/multiplication/division operations?

Hint: there is a way solve it only using (+) and (-) for arithmetics.

Pranav said...

import java.util.Scanner;
public class HelloWorld{

public static void main(String []args){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter an integer : ");
int myint = keyboard.nextInt();
while(myint>=0)
{
myint = myint - 2;
}
if(myint == -1)
{
System.out.println("Integer is odd");
}
else
{
System.out.println("Integer is even");
}

}
}

miggyJava said...

(number % 2 == 0) is even number

this should be correct?

Unknown said...

Can u pls explain me with example that if we multiply nd divide number by 2 then if number will be same then even else odd

Sajid Hussain said...

How can we get remainder of two given numbers without using % operator

Anonymous said...

number % 2 == 0

Post a Comment