Java program to check even or odd number

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.

Even number

Odd number

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.

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.

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.

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

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.

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.

The following two tabs change content below.
Working as a Java developer since 2010. Passionate about programming in Java. I am a part time blogger.

Add Comment

Required fields are marked *. Your email address will not be published.