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
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
Related Questions
Write a program to input year and check whether it is:
(a) a Leap year
(b) a Century Leap year
(c) a Century year but not a Leap year
Sample Input: 2000
Sample Output: It is a Century Leap Year.Write a program to input two unequal positive numbers and check whether they are perfect square numbers or not. If the user enters a negative number then the program displays the message 'Square root of a negative number can't be determined'.
Sample Input: 81, 100
Sample Output: They are perfect square numbers.
Sample Input: 225, 99
Sample Output: 225 is a perfect square number.
99 is not a perfect square number.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: 28A Pre-Paid taxi charges from the passenger as per the tariff given below:
Distance Rate Up to 5 km ₹ 100 For the next 10 km ₹ 10/km For the next 10 km ₹ 8/km More than 25 km ₹ 5/km Write a program to input the distance covered and calculate the amount paid by the passenger. The program displays the printed bill with the details given below:
Taxi No. :
Distance covered :
Amount :