In: Computer Science
Suppose you have three, user input temperatures (double) assigned to three objects (chem1, chem2, and chem3). How would you go about finding which of the three values entered by the user is closest to the value 240.00 WITHOUT THE USE OF LOOPS OR ARRAYS? Solve using Java Programming Language.
Explanation:
Here is the code which takes the values chem1, chem2 and chem3 from the user input and then after calculating the mod of difference of each one from 240, it decides which of the number is the closest to 240.0
Code:
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
double chem1, chem2, chem3;
Scanner sc = new Scanner(System.in);
chem1 = sc.nextDouble();
chem2 = sc.nextDouble();
chem3 = sc.nextDouble();
double diff1, diff2, diff3;
diff1 = chem1 - 240;
diff2 = chem2 - 240;
diff3 = chem3 - 240;
if(diff1<0)
diff1 = -diff1;
if(diff2<0)
diff2 = -diff2;
if(diff3<0)
diff3 = -diff3;
if(diff1<=diff2 && diff1<=diff3)
{
System.out.println(chem1+" is the closest to 240.0");
}
else if(diff2<=diff1 && diff2<=diff3)
{
System.out.println(chem2+" is the closest to 240.0");
}
else
{
System.out.println(chem3+" is the closest to 240.0");
}
}
}
Output:
PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!
PLEASE COMMENT IF YOU NEED ANY HELP!