Home > DeveloperSection > Blogs > How to find the largest and smallest number among three input numbers using Ternary Operator

How to find the largest and smallest number among three input numbers using Ternary Operator


Java Java 
Ratings:
0 Comment(s)
 490  View(s)
Rate this:

How to find the largest and smallest number among three input numbers using Ternary Operator

 

Finding the largest and the smallest number among three input numbers can be done with the help of ternary operator in java. The program code is given below.

 

package program3;

import java.io.*;

public class Program3 {

    public static void main(String[] args)throws IOException {

        int a, b,c,l,s;

        BufferedReader br= new BufferedReader (new InputStreamReader(System.in));

        System.out.println("Enter first number");

        a=Integer.parseInt(br.readLine());

        System.out.println("Enter second number");

        b=Integer.parseInt(br.readLine());

        System.out.println("Enter third number");

        c=Integer.parseInt(br.readLine());

        l=a> b ? (a > c ? a : c) : (b > c ? b : c) ;

        s=a< b ? (a < c ? a : c) : (b < c ? b : c) ;

        System.out.println("Largest number is "+l);

        System.out.println("Smallest number is "+s);

    }

}

This will produce the following output:

Enter first number

22

Enter second number

43

Enter third number

90

Largest number is 90

Smallest number is 22

 

 


Don't want to miss updates? Please click the below button!

Follow MindStick