without using if-else statement and ternary operators, accept three unequal numbers and display the second smallest number In java
Answers
Answered by
48
class Second
{
public static void main(String args[])
{
Scanner in= new Scanner(System.in);
System.out.println("Enter three unequal numbers");
int a= in.nextInt();
int b=in.nextInt();
int c=in.nextInt();
int sec= Math.max(Math.min(a,b),Math.min(Math.max(a,b),c);
System.out.println("The second smallest number is" + sec);
}
}
{
public static void main(String args[])
{
Scanner in= new Scanner(System.in);
System.out.println("Enter three unequal numbers");
int a= in.nextInt();
int b=in.nextInt();
int c=in.nextInt();
int sec= Math.max(Math.min(a,b),Math.min(Math.max(a,b),c);
System.out.println("The second smallest number is" + sec);
}
}
chandanagowda2005:
thanks..
Answered by
9
Explanation:
import java.io.*;
class Second{
public static void main(String args[])throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter first number: ");
int a = Integer.parseInt(br.readLine());
System.out.print("Enter second number: ");
int b = Integer.parseInt(br.readLine());
System.out.print("Enter third number: ");
int c = Integer.parseInt(br.readLine());
int sum = a + b + c;
int max = Math.max(a, b);
max = Math.max(max, c);
int min = Math.min(a, b);
min = Math.min(min, c);
int s = sum - max - min;
System.out.println("Second smallest number: " + s);
}
}
Similar questions