# Code In Action

## 4 Ways to check the given Integer is Even or Odd

Checking the given integer is Even or Odd is a simple practice program for novice developers. The one way is, divide the given number with 2 and if the remainder is 0 - then it is even number, else it is odd number. Beginners use this algorithm to solve the problem, but there are some other ways, by which you can solve this problem.

The 4 ways are
1. Using Modulo Operator (%) .
2. Using Division Operator ( / ).
3. Using Bitwise AND Operator (&).
4. Using Left shift and Right shift operators (<<, >>).
Let's see the code in all ways.

1. Using Modulo Operator ( % ) :

This is the most used to method to check the given number is even or odd in practice. Modulo operator is used to get the remainder of a division. For example 5 % 2 returns 1 i.e the remainder when divided 5 by 2.
So When ever, you divide a given number with 2 and if the remainder is 0 - then it is even number, else odd number.

The code snippet is below.

```private void isEvenM1(int i) {

int rem = i % 2;

if(rem == 0) {
System.out.println(i +" is Even Number");
} else {
System.out.println(i + " is Not Even Number");
}

}```

2. Using Division Operator ( / ) :

The another approach to check the given number is even or odd, is using divide operator ( / ). The algorithm is

if( (num/2) * 2 == num) {
num is even
} else {
num is odd
}

Remember, when a number is divided by 2 using ( / ) operator, it always gives quotient. So for example when you divide 5 by 2 it returns 2 as result, and 2 * 2 is 4 which is not equal to 5. Because when you divide odd numbers with 2 you always get a remainder i.e 1, in this case we are loosing it. For even numbers the remainder is always 0, so when you again multiply it with 2, it gives you the exact number.

The program by using above algorithm is below.

```private void isEvenM2(int i) {

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

}```

3. Using Bitwise AND Operator (&) :

Bitwise operators works on individual bits of a variable. for example when you perform & operation on 5 and 1 ( 5 & 1), the result is 1. Let's see how it works

5       - 0101  ( 5 in binary form ) - 5 is odd number here.
1       - 0001  ( 1 in binary form )
5 & 1 - 0001  ( result after performing bitwise & i.e 1).

If you see above, after performing bitwise & on 5 and 1, we got result 1.  Let's perform bitwise & on 4 (even number ) and 1.

4        - 0100 ( 4 in binary form) - 4 is even number here.
1        - 0001 ( 1 in binary form) - 1 is even number here.
4 & 1 - 0000 ( result is 0)

The rules of Bitwise & when performed on bits 0 and 1.

0 & 0 - 0
0 & 1 - 0
1 & 0 - 0
1 & 1 - 1

Now, lets go through the solution, if you see in above examples when you perform bitwise & on an odd number and 1, we got result 1, when you perform on even number and 1 it is 0. which means

( odd number ) & 1 is 1.
( even number ) & 1 is 0.

So the algorithm is

if( number & 1 == 0) {
even number
} else {
odd number
}

The code snippet is below.

```private void isEvenM3(int i) {

int res = i & 1;

if(res == 0) {
System.out.println(i +" is Even Number");
} else {
System.out.println(i + " is Not Even Number");
}
}```

4. Using Left shift and Right shift operators (<< , >>) :

Similar to Bitwise operators, Left shift and Right shift operators works on individual bits of a number. Let's see how Left shift operator and right shift operator works. For example,

5 >> 1 - (0101) >> 1 = 0010 i.e 2 , Here we performed right shift once. i.e 5 in binary form is 0101 and doing right shift once, it becomes 0010, as right most digit goes away. Therefore 5 >> 1 is 2. Now let's see how left shift operator works,

5 << 1 - ( 0101 ) << 1 = 1010 i.e 10. Here when you do left shift of 1 bit, the left most bit goes away.

Let's see one more example, now take 4 for our purpose.

4 >> 1 - ( 0100 ) >> 1 = 2.
4 << 1 - ( 0100 ) << 1 = 8.

Now we understand, how left shift and right shift operators works. Now let's see how to solve our problem using left and right shift operators. Now take 4, 5 for our reference.

Now i am going to perform one right shift and one left shift on both 4 and 5.

( 4 >> 1 ) << 1 = ( 2 ) << 1 = 4.
( 5 >> 1 ) << 1 = ( 2 ) << 1 = 4.

if you observe above, for even numbers, we are getting the same number again, but for odd numbers we are getting different number. We can make use of this algorithm to solve our problem.

if ( ( num >> 1) << 1 == num) {
even number
} else {
odd number
}

The code snippet is below.

```private void isEvenM4(int i) {
int res = (i >> 1) << 1; // right by 1 bit and then left shift by 1 bit
if(res == i) {
System.out.println(i +" is Even Number");
} else {
System.out.println(i + " is Not Even Number");
}

}```

Complete Program:
```package com.speakingcs.numbertheory;

public class IsEven {

public static void main(String[] args) {

IsEven ie = new IsEven();
System.out.println("Using Method 1");
ie.isEvenM1(5);
ie.isEvenM1(6);
System.out.println("Using Method 2");
ie.isEvenM2(5);
ie.isEvenM2(6);
System.out.println("Using Method 3");
ie.isEvenM3(5);
ie.isEvenM3(6);
System.out.println("Using Method 4");
ie.isEvenM4(5);
ie.isEvenM4(6);
}

private void isEvenM4(int i) {
int res = (i >> 1) << 1; // right by 1 bit and then left shift by 1 bit
if(res == i) {
System.out.println(i +" is Even Number");
} else {
System.out.println(i + " is Not Even Number");
}

}

private void isEvenM3(int i) {

int res = i & 1;

if(res == 0) {
System.out.println(i +" is Even Number");
} else {
System.out.println(i + " is Not Even Number");
}
}

private void isEvenM2(int i) {

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

}

private void isEvenM1(int i) {

int rem = i % 2;

if(rem == 0) {
System.out.println(i +" is Even Number");
} else {
System.out.println(i + " is Not Even Number");
}

}

}```

Let me know if you know other solutions.

#### 1 comment:

1. Excellent I love it.

Note: only a member of this blog may post a comment.