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

 

 

  Modified On Mar-13-2018 10:59:52 PM

Leave Comment