Write a program to Check if a number is even or odd is one of basic question which will be asked in the interview for fresher, anyone can solve this but always this question comes with a twist given at the end stating that we should not use the modulo or division operator in Java. Before getting into the actual code to begin with lets use the modulo and division operation to check if the given number is odd or even.
Check if a number is even or odd using modulo operator “%”
Modulo operator always returns the remainder, so when we divide a number by “2” and if the remainder is “zero” then it is obviously an Even number. The same we have applied in the below code.
import java.util.Scanner; public class EvenOrOddCheck { public static void main(String args[]) { int number = 0; Scanner scanner = new Scanner(System.in); System.out.println("Enter a number to check whether it is \"even\" or \"odd\""); //Read the value entered in the command prompt number = scanner.nextInt(); if((number % 2)==0) { System.out.println("The Given Number \""+number+"\" is Even"); } else { System.out.println("The Given Number \""+number+"\" is Odd"); } } }
Output:
Check if a number is even or odd using Division operator
If we divide an Even number by “2” then the result will be a whole number and multiply the result with “2” we should be getting the actual number back, whereas when we divide a Odd number by “2” the result will be fraction and when we multiply the result with “2” we will not be getting the same number again. This is the logic behind this approach.
import java.util.Scanner; public class EvenOrOddCheck { public static void main(String args[]) { int number = 0; Scanner scanner = new Scanner(System.in); System.out.println("Enter a number to check whether it is \"even\" or \"odd\""); //Read the value entered in the command prompt number = scanner.nextInt(); if(((number / 2)*2)== number) { System.out.println("The Given Number \""+number+"\" is Even"); } else { System.out.println("The Given Number \""+number+"\" is Odd"); } } }
Output:

Check if a number is even or odd without using modulo or division operators – Using Bitwise operator
We can use the Bitwise AND & operator to determine whether the given number is even or odd. Before getting into that lets get to know some basics about how bitwise operator works.
Bitwise operators Java
Bitwise operators works 1 bit at a time. Lets take two numbers 4,5 and perform the bitwise operations on it.
Bitwise AND “&” –>returns true if and only if both arguments are true
1&1 | = | 1 |
0&0 | = | 0 |
0&1 | = | 0 |
4 & 5 = 4
0100 –> 4
&
0101 —> 5
_____
0100 –> 4
Bitwise OR “|” –> returns true if at least one argument is true
1|1 | = | 1 |
0|0 | = | 0 |
0|1 | = | 1 |
0100 –> 4
|
0101 —> 5
_____
0101 –> 5
Bitwise XOR “^” –> returns true if and only if both are different.
1^1 | = | 0 |
0^0 | = | 0 |
0^1 | = | 1 |
0100 –> 4
^
0101 —> 5
_____
0001 –> 1
So when we apply Bitwise AND & over the given number and 1, if the result is equal to zero then the given number is Even if not Odd.
import java.util.Scanner; public class EvenOrOddCheck { public static void main(String args[]) { int number = 0; Scanner scanner = new Scanner(System.in); System.out.println("Enter a number to check whether it is \"even\" or \"odd\""); //Read the value entered in the command prompt number = scanner.nextInt(); if((number & 1)== 0) { System.out.println("The Given Number \""+number+"\" is Even"); } else { System.out.println("The Given Number \""+number+"\" is Odd"); } } }
Output :
Using Shift Operator
Shift operator shifts the bits, lets now see how a shift operator works.
lets take a number 4 the binary representation will be 0100.
- Left shift << : This will shift the digits towards the left 1 time.
4 << 1 : 0100 will be 1000 (8) after shifting all the bits will be moved to the left.
- Right Shift >> : This will shift the digits towards the right 1 time.
4 >> 1 : 0100 will be 0010 (2) after shifting all the bits will be moved to the right.
So number >> 1<<1== number then the number given is Even.
Lets take the a sample number 4 –> 0100
4 >>1<<1
0100 >> 1 will be 0010 (2)
0100 << 1 will be 0100 (4)
The given number is equal to the result, hence 4 is Even
Lets take another number 3 –> 0011
3 >>1<<1
0011 >>1 will be 0001 (1)
0001 <<1 will be 0010 (2)
The given number is not equal to the result, hence 3 is Odd
import java.util.Scanner; public class EvenOrOddCheck { public static void main(String args[]) { int number = 0; Scanner scanner = new Scanner(System.in); System.out.println("Enter a number to check whether it is \"even\" or \"odd\""); //Read the value entered in the command prompt number = scanner.nextInt(); if(((number >> 1)<<1)== number) { System.out.println("The Given Number \""+number+"\" is Even"); } else { System.out.println("The Given Number \""+number+"\" is Odd"); } } }
Output :
Leave a Reply