Computer Applications

Without using if-else statement and ternary operators, accept three unequal numbers and display the second smallest number.

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

Sample Input: 34, 82, 61
Sample Output: 61

Java

Java Conditional Stmts

119 Likes

Answer

import java.util.Scanner;

public class Kboat2ndSmallestNumber
{
    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 sum = a + b + c;
        int big = Math.max(a, b);
        big = Math.max(big, c);
        int small = Math.min(a, b);
        small = Math.min(small, c);
        int result = sum - big - small;
        System.out.println("Second Smallest Number = " + result);
        
    }
}

Variable Description Table

Program Explanation

Output

Answered By

49 Likes


Related Questions