In: Computer Science
in java
Write a program that convert a Fahrenheit to Celsius. For conversion, use as many as methods you want
Below is the code to convert the Fahrenheit temperature to Celsius.
-----------------------------------------------------------------------------------------------
/*
* This program converts Fahrenheit temperature to Celsius
temmperature.
* It asks for user input in Fahrenheit and displays the output in
Celsius.
*/
import java.text.DecimalFormat;
import java.util.Scanner;
public class TemperatureConvert
{
public static void main(String[] args)
{
Scanner s = new
Scanner(System.in);
System.out.print("Enter
temperature in Fahrenheit:");
double fahrenheit =
s.nextDouble();
double celsius =
convertFhToCs(fahrenheit);
//Uncomment below line
if only 2 digits after the decimal format needs to be
displayed.
//celsius =
Double.parseDouble(new
DecimalFormat("##.##").format(celsius));
System.out.println("Temperature in Celsius: "+celsius);
s.close();
}
/*
* Method to convert temperature from
Fahrenheit to Celsius.
* @param - double fh
*/
public static double convertFhToCs(double
fh)
{
//Below is the formula for
converting fahrenheit temperature to Celsius temperature.
double cs = (( 5 *(fh - 32.0)) /
9.0);
return cs;
}
}
-----------------------------------------------------------------------------------------------
Output:
The output in celsius displayed below. If you want to display only two digits after the decimal point, Please uncomment the line I kept at the line number17 and run the program.
Enter temperature in Fahrenheit:98
Temperature in Celsius:36.666666666666664