KnowledgeBoat Logo

Computer Applications

Write a program in Java to input three numbers and display the greatest and the smallest of the two numbers.

Hint: Use Math.min( ) and Math.max( )

Sample Input: 87, 65, 34
Sample Output: Greatest Number 87
Smallest number 34

Java

Java Math Lib Methods

340 Likes

Answer

import java.util.Scanner;

public class KboatGreatestNumber
{
    public static void main(String args[]) {
        
        Scanner in = new Scanner(System.in);
        
        System.out.print("Enter First Number: ");
        int a = in.nextInt();
        System.out.print("Enter Second Number: ");
        int b = in.nextInt();
        System.out.print("Enter Third Number: ");
        int c = in.nextInt();
        
        int g = Math.max(a, b);
        g = Math.max(g, c);
        
        int s = Math.min(a, b);
        s = Math.min(s, c);
        
        System.out.println("Greatest Number = " + g);
        System.out.println("Smallest Number = " + s);
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a program in Java to input three numbers and display the greatest and the smallest of the two numbers. Hint: Use Math.min( ) and Math.max( ) Sample Input: 87, 65, 34 Sample Output: Greatest Number 87 Smallest number 34

Answered By

174 Likes


Related Questions