The formula for converting a temperature from Fahrenheit to
The formula for converting a temperature from Fahrenheit to Celsius is C =5/9(F - 32) where F is the Fahrenheit temperature. Write a function named celsius that accept a Fahrenheit temperature as an argument. The function should return the temperature, converted to Celsius. Demonstrate the function by calling it in a loop that display a table of the Fahrenheit temperature 0 through 20 and their Celsius equivalents.
Solution
Temp.java
import java.text.DecimalFormat;
import java.util.Scanner;
public class Temp {
public static void main(String[] args) {
DecimalFormat df = new DecimalFormat(\"0.00\");
for(double i=0; i<=20; i++){
double celsius = celsius (i);
System.out.println(\"Fahrenheit Temparature: \"+i+\" Celsius Temparature: \"+df.format(celsius));
}
}
public static double celsius (double fahrenheit){
double celsius = ( 5 /(double) 9 ) * ( fahrenheit - 32 );
return celsius;
}
}
Output:
Fahrenheit Temparature: 0.0 Celsius Temparature: -17.78
Fahrenheit Temparature: 1.0 Celsius Temparature: -17.22
Fahrenheit Temparature: 2.0 Celsius Temparature: -16.67
Fahrenheit Temparature: 3.0 Celsius Temparature: -16.11
Fahrenheit Temparature: 4.0 Celsius Temparature: -15.56
Fahrenheit Temparature: 5.0 Celsius Temparature: -15.00
Fahrenheit Temparature: 6.0 Celsius Temparature: -14.44
Fahrenheit Temparature: 7.0 Celsius Temparature: -13.89
Fahrenheit Temparature: 8.0 Celsius Temparature: -13.33
Fahrenheit Temparature: 9.0 Celsius Temparature: -12.78
Fahrenheit Temparature: 10.0 Celsius Temparature: -12.22
Fahrenheit Temparature: 11.0 Celsius Temparature: -11.67
Fahrenheit Temparature: 12.0 Celsius Temparature: -11.11
Fahrenheit Temparature: 13.0 Celsius Temparature: -10.56
Fahrenheit Temparature: 14.0 Celsius Temparature: -10.00
Fahrenheit Temparature: 15.0 Celsius Temparature: -9.44
Fahrenheit Temparature: 16.0 Celsius Temparature: -8.89
Fahrenheit Temparature: 17.0 Celsius Temparature: -8.33
Fahrenheit Temparature: 18.0 Celsius Temparature: -7.78
Fahrenheit Temparature: 19.0 Celsius Temparature: -7.22
Fahrenheit Temparature: 20.0 Celsius Temparature: -6.67
