Program to check given input number is even or odd in java (example)

What are even & odd numbers ?

  • Given a number in java, check whether number is even or odd
  •  The numbers divisible by 2 are called even numbers
    • e.g 2, 4, 6, 8
  • The numbers not divisible by 2 are called odd numbers
    • e.g 2, 4, 6, 8
  • We will use modulus operator to check, whether given number is even or odd.
    • e.g. if input number is 5, then 5 % 2 = 1, then 5 is odd number.
    • e.g. if input number is 6, then 6 % 2 = 0, then 6 is even number.
  • We will read the input number from console using Scanner class.
    • We have used try-with-resource statement, so that Scanner can be closed automatically.

Program: check given number is even or odd in java

package org.learn;

import java.util.Scanner;

public class DemoEvenOdd {

 public static boolean checkEvenOdd(int number) {
  if (number % 2 == 0)
   return true;
  else
   return false;
 }

 public static void main(String[] args) {
  try (Scanner scanner = new Scanner(System.in)) {
   System.out.printf("Enter input number : ");
   int inputNumber = scanner.nextInt();
   System.out.printf("Is even: %b", checkEvenOdd(inputNumber));
  }
 }
}

Output: check given number is even or odd in java

Enter input number : 5
Is even number : false

Enter input number : 6
Is even number : true

Enter input number : 25
Is even number : false
Scroll to Top