Write a program in Java to find maximum of three numbers using conditional operator

This is a java program to find the maximum number out of the given three numbers by the user.

You have to enter any three integer numbers as an input from which you want to find the largest number. After that, we use the conditional operator(?, >, : ) to find the max number as the output.

import java.util.Scanner;
public class Maxofthree 
{
    public static void main(String[] args) 
    {
        int a, b, c, d;
        Scanner s = new Scanner(System.in);
        System.out.println("Enter any three numbers:");
        a = s.nextInt();
        b = s.nextInt();
        c = s.nextInt();
        d = c > (a > b ? a : b) ? c : ((a > b) ? a : b);
        System.out.println("Largest Number:"+d);
    }
}

Output:



Comments