Find Smallest Of Three Numbers Using Ternary Operator | Java Programs

Output:

*** Smallest Number Using Ternary Operator ***

Enter First Number:
1
Enter Second Number:
2
Enter Third Number:
3
Smallest Number is: 1

Click Here For Java Online Compiler

Solution:

import java.util.Scanner;

class SmallestUsingTernary {

    public static void main(String[] args) {
        int num1, num2, num3, result, temp;

        Scanner scanner = new Scanner(System.in);
        System.out.println("*** Smallest Number Using Ternary Operator ***" + "\n");
        System.out.println("Enter First Number:");
        num1 = scanner.nextInt();
        System.out.println("Enter Second Number:");
        num2 = scanner.nextInt();
        System.out.println("Enter Third Number:");
        num3 = scanner.nextInt();
        temp = num1 < num2 ? num1 : num2; //ternary
        result = num3 < temp ? num3 : temp; // ternary
        System.out.println("Smallest Number is :" + result);
    }
}
Share This :

Related Post



sentiment_satisfied Emoticon