Write a program to convert Fahrenheit to Celsius temperature in Java?

The mathematical formula to converting Fahrenheit temperature to Celsius temperature is below:

Celsius = 5 * (Fahrenheit – 32) /9

Celsius conversion program

Java Program

The following program takes the input of Fahrenheit temperature from the user. It calculates the Celsius equivalent of the Fahrenheit temperature. Displays the Celsius temperature in the output.

 

public class FahrenheitToCelsius { public static void main(String args[]) throws IOException { BufferedReader input = null; double fahrenheit,celsius; try { input = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Please enter temperature in Fahrenheit : "); fahrenheit = Double.parseDouble(input.readLine()); celsius = 5.0 * (fahrenheit - 32.0) / 9.0; System.out.println("The temperature in Fahrenheit provided:" + fahrenheit + " F"); System.out.println("The temperature in Celsius is:" + celsius + " C"); } catch(IOException ioe) { System.out.print("Unexpected IO error."); } catch(NumberFormatException nfe) { System.out.print("Please check for valid input."); } catch(Exception e) { System.out.print("Unexpected error."); } finally {
 if (input != null) { input.close(); }
} } }

Sample Output of the Program

Now run the program with sample input let’s say 32F. Calculate the output according the formula.

Celsius = 5 * (Fahrenheit – 32) /9

= 5 * ( 32- 32) / 9

= 5 * 0

=0 C

 

 

Exercise:

Now that you know to convert Fahrenheit temperature to Celsius. Write a similar program to convert Celsius to Fahrenheit temperature. Your program should take input the Celsius temperature value and convert the temperature into Fahrenheit and display the output.