Respuesta :
Answer:
Algorithm to convert temperature from Fahrenheit to Celsius:
1. declare variables "C" to store temperature in Celsius and "F" to
store the temperature in Fahrenheit.
2. Ask from user “Do you want to convert the Fahrenheit temperature to Celsius?’
3. if user input is "yes":
3.1. prompt user to enter the temperature in Fahrenheit
3.2. read the temperature in Fahrenheit and assign it variable "F"
4. Calculate the temperature in Celsius as C = (°F - 32)/1.8
5.Print the temperature in Celsius to user
6. End the program.
Here is code in java.
import java.util.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
try{
double C,F;
String user_choice;
//scanner class object to read the input
Scanner scr=new Scanner(System.in);
System.out.print("Do you want to convert the Fahrenheit temperature toCelsius?:");
// read the choice of user
user_choice = scr.nextLine();
if(user_choice.equals("yes"))
{
System.out.print("enter the temperature in Fahrenheit:");
// read the temperature in Fahrenheit
F = scr.nextDouble();
// calculate the temperature in Celsius
C = (F-32)/1.8;
// print the temperature in Celsius
System.out.println("Corresponding temperature in Celsius is: "+C);
}
}catch(Exception ex){
return;}
}
}
Explanation:
Read the choice of user.If the user_choice is equal to "yes", then read the temperature in Fahrenheit and calculate Corresponding temperature in Celsius as "C = (°F - 32)/1.8". Print the temperature in Celsius.
Output:
Do you want to convert the Fahrenheit temperature toCelsius?:yes
enter the temperature in Fahrenheit:68
Corresponding temperature in Celsius is: 20.0