TO FIND THE GREATEST OF THREE NUMBERS USING TERNARY code example

Example 1: Java program to find largest in three numbers using ternary operator

// Java program to find largest in three numbers using ternary operator
import java.util.Scanner;
public class LargestInThree 
{
   public static void main(String[] args) 
   {
      int number1, number2, number3, largest;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter first number: ");
      number1 = sc.nextInt();
      System.out.println("Please enter second number: ");
      number2 = sc.nextInt();
      System.out.println("Please enter third number: ");
      number3 = sc.nextInt();
      largest = number3 > (number1 > number2 ? number1 : number2) ? number3 : ((number1 > number2) ? number1 : number2);
      System.out.println("Largest in three numbers using ternary operator: " + largest);
      sc.close();
   }
}

Example 2: calculate max of three numbers using ternary operator in c

big = a > b ? ( a > c ? a : c) : (b > c ? b : c) ;

Tags:

Java Example