Find max and min number - Java Program

Q. Write a java program to find maximum and minimum number from given three numbers using conditional operator.

Answer:

Conditional operator also works like if else statement. It is used to check the condition of the expression.
In this example, the condition operator compares between the three number and find the maximum and minimum number.

FindMinMax.java

class FindMinMax
{
        public static void main(String[] args)
        {
                int a = 12;
                int b = 18;
                int c = 15;
                System.out.println("The Numbers are a= "+a+" b= "+b+" c= "+c);
                int max = (a>b ? (a>c ? a : c): (b>c ? b : c));
                System.out.println("Max= "+max);
                int min = (a<b ? (a<c ? a : c): (b<c ? b : c));
                System.out.println("Min= "+min);
        }
}


Output:

max min number