Even odd program in java using ternary operator

In this post let’s learn even odd program in java using ternary operator.

Even odd program in java using ternary operator

Let’s see how to check if a number is even in java or how to check if a number is odd in java,

Also read – Even odd program in java using array

// java check if number is even
import java.util.Scanner;

public class EvenOddTernary 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter a number : ");
      int number = sc.nextInt();
      // java odd or even
      String strInput = (number % 2 == 0) ? "even" : "odd";   
      System.out.println(number + " is " + strInput);
      sc.close();
   }
}

Output:

Please enter a number : 15
15 is odd