Organizational Research By

Surprising Reserch Topic

Question:How to Check If Number is Even or Odd without using Modulus or Remainder Operator



asked Sep 13, 2013 in JSP by rajesh
edited Sep 12, 2013
0 votes
143 views



Related Hot Questions

2 Answers

0 votes

 

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 complete Java program to test if number is even or odd without using modulus operator . 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
 
answered Sep 13, 2013 by rajesh
edited Sep 12, 2013
0 votes

 

Even or Odd using division 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");           
}
 
answered Sep 13, 2013 by rajesh
edited Sep 12, 2013

...