blog

Home / DeveloperSection / Blogs / How to find the largest and smallest number among three input number using Math.max() and Math.min()

How to find the largest and smallest number among three input number using Math.max() and Math.min()

Devesh Kumar Singh1837 18-Nov-2015

Finding the largest and the smallest number among three input numbers can be done with the help of max and min methods in java. These are math functions provided by the Math class. The program code is given below.

 

package program2;

import java.io.*;

public class Program2 {

    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=Math.max(Math.max(a,b), c);

       

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

        s=Math.min(Math.min(a, b), c);

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

       

    }

}

 

It will produce the following Output:

Enter first number

45

Enter second number

3

Enter third number

76

Largest number is 76

Smallest number is 3


Updated 13-Mar-2018

Leave Comment

Comments

Liked By