Java program to find even or odd

Today you will learn java program to find even or odd.

Java program to check if a given number is even or odd

In the below java program to check if a given integer is odd or even, modulus operator is used. Here user enters a number as input.

Also read – even odd program in java using array

This input number is divided by 2. If the remainder is equal to 0 then the number entered is even number else the number entered is odd number.

import java.util.Scanner;

public class EvenOdd
{
   public static void main(String[] args)
   {
      int a;
      System.out.println("Please enter a number to check even or odd : ");
      Scanner sc = new Scanner(System.in);
      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");
      }
   }
}

Output:

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


Java program to check even or odd number

Now let’s learn another java program to find even or odd. In this program user enters a number.

Entered number is first divided by two and then multiplied by two. If the result is same as original number then number is even number else odd number.

For example: entered number 18, 18/2 = 9 then 9*2 =18

import java.util.Scanner;

public class EvenOddDemo
{
   public static void main(String[] args)
   {
      int num;
      System.out.println("Please enter a number : ");
      Scanner sc = new Scanner(System.in);
      num = sc.nextInt(); 
      if((num/2)*2 == num)
      {
         System.out.println("Even number");
      }
      else
      {
         System.out.println("Odd number");
      }
      sc.close();
   }
}

Output:

java program to find even or odd