Java Program to find the Smallest of three numbers using Ternary Operator

The following example of Java program to find the smallest of three numbers using the ternary operator :
Program :
import java.util.Scanner;

public class SmallestOfThreeNumbersUsingTernary {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // Read three numbers from the user
        System.out.print("Enter the first number: ");
        int num1 = sc.nextInt();

        System.out.print("Enter the second number: ");
        int num2 = sc.nextInt();

        System.out.print("Enter the third number: ");
        int num3 = sc.nextInt();

        // Find the smallest of the three numbers using the ternary operator
        int smallest = num1 < num2 ? (num1 < num3 ? num1 : num3) : (num2 < num3 ? num2 : num3);

        // Print the smallest number
        System.out.println("The smallest of the three numbers is " + smallest);
    }
}
Output :
Enter the first number: 9
Enter the second number: 17
Enter the third number: 5
The smallest of the three numbers is 5
Explanation :

* We first import the Scanner class to read input from the user.

* We then create an instance of the Scanner class to read input from the standard input stream.

* We read three numbers from the user using the nextInt() method of the Scanner class.

* We use the ternary operator to find the smallest of the three numbers.

* Finally, we print the smallest number using the println() method of the System.out object.