Monday 28 May 2018

Find number is even or odd without using % operator in Java




Sample Program:-


 public class OddOrEven {  
      public static void main(String[] args) {  
           int number = 2;  
           if ((number & 1) == 0)  
                System.out.println("EVEN");  
           else  
                System.out.println("ODD");  
      }  
 }  

Output:-


 EVEN  

Program Logic:-
Binary number consists of only 0's and 1's ,
if last digit is 0, then number is EVEN in java.

if last digit is 1, then number is ODD in java.
For ex:20 is 10100
25 is 11001

Enjoy Coding.

No comments:

Post a Comment