KnowledgeBoat Logo

Computer Applications

Write a program to input three unequal numbers. Display the greatest and the smallest number.

Sample Input: 28, 98, 56
Sample Output: Greatest Number: 98
                           Smallest Number: 28

Java

Java Conditional Stmts

143 Likes

Answer

import java.util.Scanner;

public class KboatMinMaxNumbers
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter 3 unequal numbers");
        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 min = a, max = a;
        min = b < min ? b : min;
        min = c < min ? c : min;
        
        max = b > max ? b : max;
        max = c > max ? c : max;
        
        System.out.println("Greatest Number: " + max);
        System.out.println("Smallest Number: " + min);
        
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a program to input three unequal numbers. Display the greatest and the smallest number. Sample Input: 28, 98, 56 Sample Output: Greatest Number: 98 Smallest Number: 28

Answered By

54 Likes


Related Questions