Write a Java program that uses a Scanner object to read in t
Write a Java program that uses a Scanner object to read in three numbers and determines the smallest and largest numbers without using Java\'s Math Class.
Ask a user to enter three numbers. Then print the sum of the three numbers, the largest of the three numbers and the smallest of the three numbers. Finally, print all three numbers from largest to smallest. See the sample below for proper formatting of the output. Do not concern yourself with controlling the display of the decimal point. You can assume all three numbers are different.
Sample:
Enter three numbers: 22 10 30
The sum of 22.0, 10.0, and 30.0 is 62.0
The largest number is 30.0
The smallest number is 10.0
The numbers from largest to smallest are: 30.0, 22.0, and 10.0
Solution
Hi, Please find my implementation.
Please let me know in case of any issue:
import java.util.Scanner;
public class LargestSmallest {
public static void main(String[] args) {
// creating Scanner Object
Scanner sc = new Scanner(System.in);
double x, y, z; // variable to store three input
double largest, smallest, middle; // variable to store largest, smallest and middle element
// taking user input
System.out.print(\"Enter three numbers:\");
x = sc.nextDouble();
y = sc.nextDouble();
z = sc.nextDouble();
// finding largest , smallest and middle
if(x > y){
if(x > z){
largest = x;
if(y > z){
middle = y;
smallest = z;
}
else{ //z >= y
middle = z;
smallest = y;
}
}
else{ // z >= x
largest = z;
middle = x;
smallest = y;
}
}
else{ // y >= x
if(y > z){
largest = y;
if(x > z){
middle = x;
smallest = z;
}
else{ // z >= x
middle = z;
smallest = x;
}
}else{// z >= y
largest = z;
middle = y;
smallest = x;
}
}
System.out.println(\"The sum of \"+x+\", \"+y+\", and \"+z+\" is \"+(x+y+z));
System.out.println(\"The largest number is \"+largest);
System.out.println(\"The smallest number is \"+smallest);
System.out.println(\"The numbers from largest to smallest are: \"+largest+\", \"+middle+\", and \"+smallest);
}
}
/*
Sample output:
Enter three numbers:22 10 30
The sum of 22.0, 10.0, and 30.0 is 62.0
The largest number is 30.0
The smallest number is 10.0
The numbers from largest to smallest are: 30.0, 22.0, and 10.0
*/



