Java program to check even or odd number

Let’s learn java program to check even or odd number.

Java program to check even or odd number

In the below even odd program in java first user enters a number as input using nextInt() method of Scanner class.

This number is stored in integer variable ‘a’. Now this user entered number is divided by 2. For this we use modulus operator.

Then inside if else if the remainder is equal to 0 then the number entered is even number. If remainder is equal to 1 then number entered is odd number. Now let’s see an example to check whether given number is even or odd using if else.

import java.util.Scanner;
public class FindEvenOrOdd
{
   public static void main(String[] args)
   {
      int a;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter a number to check even or odd: ");
      a = sc.nextInt();
      if(a % 2 == 0)
      {
         System.out.println("Entered number is an even number.");
      }
      else
      {
         System.out.println("Entered number is an odd number.");
      }
      sc.close();
   }
}


Output:

Please enter a number to check even or odd: 5
Entered number is an odd number.