In this program instead of using IF/ELSE statement or any other condition to prove a number is even or odd, we will use Ternary operator. Ternary operator makes it easy and uses less number of lines of code.

Program:

import java.util.Scanner;

public class EvenOdd
{
    public static void main(String[] args)
    {
      Scanner in = new Scanner(System.in);
      System.out.println("\nEnter a number to check whether it's even / odd\n");
      int a = in.nextInt();
      System.out.println((a%2==0)?a+" is an even number":a+" is an odd number");
      //Ternary operator used.
    }
}

Output:

Enter a number to check whether it's even / odd

124
124 is an even number

You can download the pdf file of EvenOdd.

Advertisements