Java – Check whether number is Even or Odd ?

In this article, we will write a simple Java program to check whether passed/input number is Even or Odd

1. Check Number is Even or Odd

  • We will evaluate the given number using if-else statements,
    1. If number is divided by 2 and remainder is Zero then number is Even
    2. Else number is Odd

CheckNumberEvenOrOddOrZero.java

package in.bench.resources.numbers;

public class CheckNumberEvenOrOddOrZero {

	// main method
	public static void main(String[] args) {

		checkNumberIsEvenOrOdd(19); // odd
		checkNumberIsEvenOrOdd(0); // zero
		checkNumberIsEvenOrOdd(20); // even
	}


	/**
	 * This method prints whether number is Odd or Even or Zero
	 * 
	 * @param number
	 */
	public static void checkNumberIsEvenOrOdd(int number) {

		if(number%2 == 0) {

			// number is Even
			System.out.println(number + " is an Even number !!");
		}
		else {

			// number is Odd
			System.out.println(number + " is an Odd number !!");
		}
	}
}

Output:

19 is an Odd number !!
0 is an Even number !!
20 is an Even number !!

Happy Coding !!
Happy Learning !!

Java - How to Reverse a Number ?
Java - Check whether number is Positive or Negative or Zero ?