Even Number Program in Java

Even Number Program in Java | Write a Java program to check If the given number is an even number or not. To check even numbers in Java we can use an if-else statement or ternary condition. There are many different ways to write an even number program in java using them we check the number is even or not.

Even number:- A number is called an even number if it is divisible by 2. Example:- 4,8,12 e.t.c. 9,11 are not an even number because they are not completely divisible by number 2. The opposite of an even number is an odd number.

import java.util.Scanner;

public class CheckEven {

   public static boolean isEven(int n) {
      // if number is divisible by 2
      // then return true
      if(n % 2 == 0) return true;

      // else return false
      return false;
   }

   public static void main(String[] args) {

      // declare variable
      int number = 0;
      boolean result = false;

      // create Scanner class object to take input
      Scanner scan = new Scanner(System.in);

      // read number from end-user
      System.out.print("Enter an integer number:: ");
      number = scan.nextInt();

      // check number is even or not
      result = isEven(number);

      // display result
      if(result) 
          System.out.println(number+" is an even number");
      else 
          System.out.println(number +
                  " is not an even number");

      // close Scanner class object
      scan.close();
   }
} 

Output for Test-case-1:-

Enter an integer number:: 12
12 is an even number

Output for Test-case-2:-

Enter an integer number:: 9
9 is not an even number

In this program, we used the if-else statement to check the number is even or not. The if-else statement in Java is a selection statement. It is used to check the given condition is true or false. If-block statement is executed when test expression evaluated to true. When the test expression evaluates to false, control goes to the next executable statement.

Also see:- Special number, Magic number, Armstrong number, Perfect number, Evil Number, Spy Number, Sunny number in Java

Different ways to check even number in Java

Using ternary operator

Similarly like if-else, we can also use the ternary operator to check number is even or not. Below given isEven() method demonstrate it,

public static boolean isEven(int n) {
     return (n%2==0) ? true: false;
} 

For the ternary operator:- In the given below expression,

(expression1) ? expression2 : expression3;

If the expression1 is true then it returns the result of expression2 else it returns the result of expression3. Here if the number is divisible by 2 then true will be returned to the main method else false will be returned to the main method.

Check even number in Java using switch case

public static boolean isEven(int n) { 
    int option = n % 2;
    switch(option) {
      case 0:
          return true;
      case 1:
          return false;
    }
    return false;
} 

The switch case statement is a multi-way decision that tests whether an expression matches one of a number of constant integer values, and branches accordingly.

Check even number without using modulus(%) operator

If you want to write a program to check even number without using a mod or modulus (%) operator in Java then the division operator and multiplication operator can be used to solve this problem.

public static boolean isEven(int n) {
   if((n/2)*2 == n) return true; //even
   return false; //not even
}

When a number is completely divisible by 2 then multiplication of the result by 2 gives the same number then the number is even, else the number is not an even number.

Program to print even number in Java within a range

In Java for a given range, we can use a loop to check whether the numbers are even or not. Previously we have discussed different ways to check the number is even or not. You can use any one of them. We used ternary operator because it is simple, understandable and short.

import java.util.Scanner;

public class CheckEvenInRange {

   public static boolean isEven(int n) {
      return (n%2==0) ? true: false;
   }

   public static void main(String[] args) {

      // declare variable
      int minRange = 0, maxRange = 0;
      boolean result = false;

      // create Scanner class object to take input
      Scanner scan = new Scanner(System.in);

      // read number from end-user
      System.out.print("Enter min range:: ");
      minRange = scan.nextInt();
      System.out.print("Enter max range:: ");
      maxRange = scan.nextInt();

      // check number is even or not in range
      System.out.println("Even numbers are: ");
      for(int i = minRange; i <= maxRange; i++) {
          result = isEven(i);
          // display result
          if(result) 
              System.out.print( i +" ");
      }

      // close Scanner class object
      scan.close();
   }
} 

Output:-

Enter min range:: 10
Enter max range:: 25
Even numbers are:
10 12 14 16 18 20 22 24

In this program, we have used for loop. The for loop is a pre-test loop, where first of all initialization expression is evaluated then the condition is checked and if the condition is true then only the statements of the for loop are executed.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *