How to Find Even and Odd Number in Java - Program Tutorial Example (original) (raw)

Even and Odd number check Java Example

There are many ways to find if a number is even or odd in Java but before moving into technical details on finding even and odd numbers in Java let's what is even and odd numbers in terms of Mathematics. Any number which is completely divisible by 2 is called an even number while a number that is not completely divisible by 2 is called an odd number. If you think in terms of remainder then in the case of even number, the remainder is zero while in the case of odd number remainder will be 1. zero is considered as an even number in maths.

We can use this property to find whether a number is even or odd in Java. Java has a remainder operator also called modules operation denoted by % which does exactly the same. it returns remainder as a result of the division. here is a Java sample program of finding even and odd numbers using remainder and bitwise AND operator in Java

Java program to find even and odd numbers in Java

As I said there are multiple ways to check if a number is even or not in Java and if a number is not even then it certainly be odd. Like you can use a division operator in a loop and start from 1 keep multiplying it by 2 until you cross the number if the number matches then it's an even number otherwise it's an odd number.

Another way of finding even and an odd number is by using the bitwise operator in Java. In binary format, the even number has there LSB always zero while the odd number has there LSB as 1 by using this information and bitwise & operator we can find if a number is even or odd in Java.

How to find even and odd number in Java - Program tutorial example

By the way, in this example, we will see two ways to check if a number is odd or even, first by using the remainder operator and second by using the bitwise AND operator in Java.

package example;

import java.util.Scanner;

/**
* Java program to find if a number is even or odd in Java or not. This Java program

* example demonstrates two ways to check if the number is even or odd or not, the first example

* uses modulus or remainder operator denoted by % to see if the number is even or not

* and the second operator uses Bitwise AND operator to find if the number is even or odd in Java.
*
* @author Javin Paul
*/
public class EvenOddTest{

public static void main(String args[]){

//scanner to get input from user
Scanner console = new Scanner(System.in);

System.out.printf("Enter any number : ");

//return the user input as integer
int number = console.nextInt();

//if remainder is zero than even number
if((number %2)==0){
System.out.printf("number %d is even number %n" , number); //%d -decimal %n new line

} else{
//number is odd in Java
System.out.printf("number %d is odd number %n", number);
}

//Finding Even and Odd number using Bitwise AND operator in Java.

System.out.printf("Finding number if it's even or odd using bitwise AND operator %n");

//For Even numbers
//XXX0
//0001 AND
//0000
if( (number&1) == 0){
System.out.printf("number %d is even number %n" , number);
}else{
System.out.printf("number %d is odd number %n", number);
}

}

}
Output:
Enter any number: 17
number 17 is an odd number
Finding number if its even or odd using bitwise AND operator
number 17 is an odd number
Enter any number: 12
number 12 is an even number
Finding number if its even or odd using bitwise AND operator
number 12 is an even number

That’s all on How to check if a number is even or odd in Java. Surely there are many other ways to check if a number is odd or even and you can certainly find a more innovative and creative ways of doing it but this is something very useful to know.

Sometimes interviewer twists it a little bit and said how do you check if a number is even or odd without using an arithmetic operator and remainder operator, well bit-wise AND is your option which is also the fastest method to check even and odd in Java.

Thanks for reading this article. If you like then please share it with your friends and colleagues. If you have any questions or feedback, please drop a note. If you have any questions or doubt then please let us know and I'll try to find an answer for you. Btw, what is your favorite coding exercise? prime number, palindrome or this one?