ICSE Class 10 Computer Applications Question 21 of 43

Conditional Constructs in Java — Question 21

Back to all questions
21
Question

Question 21

Using the ternary operator, create a program to find the largest of three numbers.

Answer
import java.util.Scanner;

public class KboatLargestNumber
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter first number: ");
        int a = in.nextInt();
        System.out.print("Enter second number: ");
        int b = in.nextInt();
        System.out.print("Enter third number: ");
        int c = in.nextInt();
        
        int max = a > b ? a : b;
        max = max > c ? max : c;
        
        System.out.print("Largest Number = " + max);
    }
}
Output
BlueJ output of KboatLargestNumber.java