Odd-Even Program in Java

In this post, we will develop the even-odd program in Java. There are different ways to check or find even or odd numbers. Previously we had developed an even number program to check number is even or not, and an odd number program to check number is odd or not. Now, we will check both in a single program.

Up to now, you learned how to write a program to check number is even or not, and then check number is odd or not. If you combine both of the programs then it will a new program which checks the number is even or odd. There are various methods to write even odd program in Java.

import java.util.Scanner;

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

      // else the number is odd
      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 = isOddEven(number);

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

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

The output of the different test-cases:-

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

Enter an integer number:: 9
9 is an odd 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.

When the number is divisible by 2 then if block will be executed otherwise else block will be executed.

Odd or Even using Switch Case in Java

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.

import java.util.Scanner;

public class OddEven {

   public static boolean isOddEven(int n) {
      int option = n % 2;
      switch(option) {
      case 0:
          return true;
      case 1:
          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 = isOddEven(number);

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

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

Odd or Even using Ternary Operator

import java.util.Scanner;

public class OddEven {

   public static boolean isOddEven(int n) {
      return (n % 2 == 0) ? true : 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 = isOddEven(number);

      // display result
      if(result) 
          System.out.println(number+" is an even number");
      else 
          System.out.println(number+" is an odd number");
 
     // close Scanner class object
     scan.close();
   }
}

Even Odd Program in Java using For Loop

We can also write even odd program in java using for loop to check print even and odd numbers within a range.

import java.util.Scanner;

public class OddEvenInRange {

   public static boolean isEvenOdd(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();

      // display odd even
      for(int i = minRange; i <= maxRange; i++) {
          result = isEvenOdd(i);
          // display result
          if(result) 
             System.out.print(i+":Even \t");
          else
             System.out.print(i+":Odd \t");
      }

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

Output:-

Enter min range:: 10
Enter max range:: 25
10:Odd 11:Even 12:Odd 13:Even 14:Odd 15:Even 16:Odd 17:Even 18:Odd 19:Even 20:Odd 21:Even 22:Odd 23:Even 24:Odd 25:Even

Even or Odd Program without using Modulus Operator(%) in Java

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

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

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 odd.

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 *