## Java program to check even or odd number

Java program to check even or odd number is Java programming interview question asked to freshers. It will be easy to solve using modulus or division operator. But the interviewer may trick you to write Java program to check even or odd number without using modulus and division operator. Here we will see multiple ways of solving it.

For a definition of even and odd number check the below links.

### Program to check even or odd number using modulus operator

It is easy to check if a number is even or odd using modulus operator. Here is the program.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.Scanner; public class CheckEvenOdd { public static void main(String[] args) { int number; Scanner in = new Scanner(System.in); System.out.println("Enter a number to check even or odd"); number = in.nextInt(); if (number % 2 == 0) { System.out.println(number + " is Even number"); } else { System.out.println(number + " is Odd Number"); } in.close(); } } Output: Enter a number to check even or odd 28 28 is Even number |

### Program to check even or odd number using division operator

The logic is simple here. Divide the given number by 2 and multiply the result by 2. If the result equals the given number then it is even, otherwise an odd number.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.Scanner; public class CheckEvenOdd { public static void main(String[] args) { int number; Scanner in = new Scanner(System.in); System.out.println("Enter a number to check even or odd"); number = in.nextInt(); int temp = number / 2; if (temp * 2 == number) { System.out.println(number + " is Even number"); } else { System.out.println(number + " is Odd Number"); } in.close(); } } Output: Enter a number to check even or odd 35 35 is Odd Number |

### Program to check even or odd number using bitwise operator

Not many of you know about this logic. The logic here is to use bitwise AND operator. The bitwise AND operator (&) performs logical AND operation with each binary digits of the given numbers. For example,

Number 2 can be represented in binary as 010 and number 3 can be represented as 011. Now the bitwise AND operation between 2 and 3 is as follows,

010

011

—–

010

—–

Now if you do bitwise AND operation between a given number and the number 1, then the result will be 0 if the given number is even and the result will be 1 if the given number is odd. For example,

010 011 1101

001 001 0001

—– —– ——

000 001 0001

—– —– ——

Above is the logic to be used in the program. So when the interviewer asks to write a Java program to check even or odd number without using modulus and division operator then you can use this logic.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.Scanner; public class CheckEvenOdd { public static void main(String[] args) { int number; Scanner in = new Scanner(System.in); System.out.println("Enter a number to check even or odd"); number = in.nextInt(); if ((number & 1) == 0) { System.out.println(number + " is Even number"); } else { System.out.println(number + " is Odd Number"); } in.close(); } } Output: Enter a number to check even or odd 105 105 is Odd Number |

The problem can also be solved using bitwise shift operator. Use the below logic,

1 | (n>>1)<<1 == n ; // true if even number |

### Program to check even or odd number without using modulus, division and bitwise operators

What if the interviewer goes a level beyond and asks you to write a program to check even or odd number without using modulus, division and bitwise operators? It will be really tricky and will be difficult to get the logic during the interview. Here I have solved it using repeated subtraction. Let us see the program.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | import java.util.Scanner; public class CheckEvenOdd { public static void main(String[] args) { int number; Scanner in = new Scanner(System.in); System.out.println("Enter a number to check even or odd"); number = in.nextInt(); int temp = number; if (temp < 0) { temp = -temp; } String st = String.valueOf(temp); int stlen = st.length(); while (stlen >= 1) { int sub = 1; for (int i = 1; i < stlen; i++) { sub = sub * 10; } if (sub == 1) { sub = 2; } while (temp >= sub) { temp = temp - sub; } st = String.valueOf(temp); stlen = st.length(); if (temp < 2) { break; } } if (temp == 0) { System.out.println(number + " is Even number"); } else { System.out.println(number + " is Odd Number"); } in.close(); } } Output: Enter a number to check even or odd 252 252 is Even number |

Here is the explanation of the logic used in the above program.

- If the given number is a single digit number and greater than 2 then it will be repeatedly subtracted by 2 until it becomes less than 2. If the given number is an even number, then the repeated subtraction will yield a result of 0 whereas if the number is odd then the subtraction will yield 1 as the final result.
- If the given number is a double digit number then it will be repeatedly subtracted by 10 (which is the minimum double digit number) until it becomes less than 10 (single digit) and the result will then be repeatedly subtracted by 2 as mentioned in the above point.
- If the given number is a three digit number then it will be repeatedly subtracted by 100 until it becomes less than 100 and the above steps continues.
- Similar logic applies for other higher numbers.
- If the number is negative, it is converted to a positive number and then the above steps apply.

Note that the above program can be simply written by only using repeated subtraction by 2, but it will lead to higher execution time for larger numbers.

There can also be many other ways to solve this problem. Hope the logic discussed in this post is useful. If you have any comments, post it in the comments section.

#### Latest posts by Uday (see all)

- How to run shell commands using SSH connection in Java - July 26, 2017
- Java program to print rectangle of stars - July 20, 2017
- Introduction to Java Enum data type with example programs - March 31, 2017