16 Jan 2018

Program to Check even or odd without using modulus and division operators.


To check Even or Odd we go through the checking of number that number is divisible by 2 or not if given number is divisible by 2 then we say that the given number is Even otherwise it is odd.

Before going to actual program lets see how to check a number is even or odd by using modulus and division operators.

Here, we will see the basic idea of checking even or odd.

Program #1 (By Using Modulus Opertaor)  

import java.util.Scanner;

public class EvenOrOdd {

          public static void main(String[] args) {

                   int number;
                   System.out.println("Enter a number to check even or odd");
                   number = new Scanner(System.in).nextInt();

                   if ((number % 2) == 0) {
                             System.out.println(+number + " is Even number");
                   } else {
                             System.out.println(+number + " is Odd Number");
                   }

          }
}

Output:

Enter a number to check even or odd
20
20 is Even number
Press any key to continue . . .

Program #2(By Using Division Operator )  

import java.util.Scanner;

public class EvenOrOdd {

          public static void main(String[] args) {

                   int number;
                   System.out.println("Enter a number to check even or odd");
                   number = new Scanner(System.in).nextInt();

                   if ((number / 2) * 2 == number) {
                             System.out.println(+number + " is Even number");
                   } else {
                             System.out.println(+number + " is Odd Number");
                   }

          }
}

Output:

Enter a number to check even or odd
23
23 is Odd Number
Press any key to continue . . .

So form above we see that we have used the modulus(%) and division(/) operator to check the even and odd number. let see the other ways where we will not use the modulus(%) or division(/) operators to check Even or Odd number.

1) By using Shift Operator
2) By using Bitwise Operator

Program #3 (By using Shift Operator)

uses of shift operator can be alternative of checking the given number is even or odd.in this process we will follow the below steps.

Step 1: Take the number and perform the right shift on it one time.

            Ex:  2 >> 1 =1
Here 2 means 010 and when we apply right shift once then it will become 001 which means number becomes 1.

Step 2: Now again apply the left shift once on this number which is 1 means (001) we will get 2.
            
           Ex:  1 << 1 =2

Step 3:  check this number with actual number if number is same then it will be even otherwise odd number. 

import java.util.Scanner;

public class EvenOrOdd {

          public static void main(String[] args) {

                   int number;
                   System.out.println("Enter a number to check even or odd");
                   number = new Scanner(System.in).nextInt();

                   if ((number >> 1) << 1 == number) {
                             System.out.println(+number + " is Even number");
                   } else {
                             System.out.println(+number + " is Odd Number");
                   }
          }
}

Output

Enter a number to check even or odd
33
33 is Odd Number
Press any key to continue . . .

Enter a number to check even or odd
32
32 is Even number
Press any key to continue . . .

Program #4 (By using Bitwise Operator)

Here we are going to use Bitwise operators like & and | .this operator works like ,it compare the bit by bit of numbers.here we are going to compare each number with 1 by using bitwise & and | if it will give 0 then number is Even and if its gives other then number is Odd.
Lets see how Bitwise operator works.
  1. AND : 1 & 1=1
  2. OR   :     0 | 1= 1 , 1 | 0=1 , 1| 1= 1
  3. XOR :   0 ^ 1= 1 , 1^ 0=1
  4. NOT : !0=1


 Truth Table for Bitwise operators (AND,OR ,XOR,NOT)

A
B
A&B
A|B
A^B
~A
0
0
0
0
0
1
0
1
0
1
1
1
1
0
0
1
1
0
1
1
1
1
0
0


import java.util.Scanner;

public class EvenOrOdd {

          public static void main(String[] args) {

                   int number;
                   System.out.println("Enter a number to check even or odd");
                   number = new Scanner(System.in).nextInt();

                   if ((number & 1) == 0) {
                             System.out.println(+number + " is Even number");
                   } else {
                             System.out.println(+number + " is Odd Number");
                   }
          }
}
Output:

Enter a number to check even or odd
88
88 is Even number
Press any key to continue . . .