1// Java program to find smallest of three numbers using ternary operator
2import java.util.Scanner;
3public class SmallestOfThreeNumbers
4{
5 public static void main(String[] args)
6 {
7 int number1, number2, number3, smallest, temp;
8 Scanner sc = new Scanner(System.in);
9 System.out.println("Please enter first number: ");
10 number1 = sc.nextInt();
11 System.out.println("Please enter second number: ");
12 number2 = sc.nextInt();
13 System.out.println("Please enter third number: ");
14 number3 = sc.nextInt();
15 sc.close();
16 temp = number1 < number2 ? number1 : number2;
17 smallest = number3 < temp ? number3 : temp;
18 System.out.println("Smallest of three numbers using ternary operator: " + smallest);
19 }
20}
1// Java program to find largest in three numbers using ternary operator
2import java.util.Scanner;
3public class LargestInThree
4{
5 public static void main(String[] args)
6 {
7 int number1, number2, number3, largest;
8 Scanner sc = new Scanner(System.in);
9 System.out.println("Please enter first number: ");
10 number1 = sc.nextInt();
11 System.out.println("Please enter second number: ");
12 number2 = sc.nextInt();
13 System.out.println("Please enter third number: ");
14 number3 = sc.nextInt();
15 largest = number3 > (number1 > number2 ? number1 : number2) ? number3 : ((number1 > number2) ? number1 : number2);
16 System.out.println("Largest in three numbers using ternary operator: " + largest);
17 sc.close();
18 }
19}