How to find maximum and minimum numbers using Ternary Operators?

Introduction:

Hii everyone, here we are going to use the most interesting and efficient method for programming languages which is ternary operators.Ternary operators are generally used to compare different numbers, we are having some other alternatives as well to find the maximum numbers and minimum numbers but this is most efficient one to make your program short and attractive.

Description Of Program:

Here we are making our program user flexible so we have to take scanner input and in this, we are taking three variables named as (a,b,c) which is carrying integer data type.Moving forward we prepared two statements to find maximum and minimum numbers, try to understand the logic behind the given statements using a flowchart that will be helpful to understand the whole logic and then print the output. Hope you find it helpful.                                                                                   Thank You

CODE:

import java.util.Scanner;

public class Terinarymax {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int a,b,c,max,min;
System.out.println(“Enter the value of a,b,c”);
a=s.nextInt();
b=s.nextInt();
c=s.nextInt();
max=(a>=b)?(a>=c)?a:c:(b>=c)?b:c;
min=(a<=b)?(a<=c)?a:c:(b<=c)?b:c;
System.out.println(“max=”+max);
System.out.println(“min=”+min);

}

}

OUTPUT :

Leave a Reply