14,210,385 members
Technical Blog
Posted 1 Jul 2015

15.2K views
1 bookmarked

# 4 Ways to Check whether the Given Integer is Even or Odd

,
Rate this:
1 Jul 2015     CPOL
Here are 4 ways to check whether 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 an even number, else it is an 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 method to check whether 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 whenever you divide a given number by 2 and if the remainder is 0 - then it is an even number, else it is an 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 ( / )

Another approach to check whether 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 a 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 losing it. For even numbers, the remainder is always 0, so when you again multiply it by 2, it gives you the exact number.

The program by using the 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 work on individual bits of a variable. For example, when you perform an 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, let's go through the solution, if you see in the 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 work on individual bits of a number. Let's see how Left shift operator and right shift operator work. 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 a 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 any other solutions.

## Share

 India
No Biography provided

 First Prev Next
 Performance numbers Dave Black3-Dec-18 8:38 Dave Black 3-Dec-18 8:38
 My vote of 5 José Cintra26-Oct-15 5:38 José Cintra 26-Oct-15 5:38
 What about performance Lucas D. Luengo1-Jul-15 5:05 Lucas D. Luengo 1-Jul-15 5:05
 My vote of 1 OriginalGriff1-Jul-15 4:38 OriginalGriff 1-Jul-15 4:38
 My vote of 1 Nagy Vilmos1-Jul-15 3:30 Nagy Vilmos 1-Jul-15 3:30
 Re: My vote of 1 Dave Black3-Dec-18 7:49 Dave Black 3-Dec-18 7:49
 Last Visit: 18-Jun-19 15:26     Last Update: 18-Jun-19 15:26 Refresh 1

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Web01 | 2.8.190618.1 | Last Updated 1 Jul 2015