How to convert Fahrenheit to Celsius Program in Java

Here is an explaination on How to convert Fahrenheit to Celsius Program in Java.

import java.util.*;
class FahrenheitToCelsius 
{
public static void main(String[] args) 
{
float temperature;
Scanner in = new Scanner(System.in);
System.out.println("Enter temperatue in Fahrenheit"); temperature=in.nextInt();
temperature = ((temperature - 32)*5)/9;
System.out.println("Temperature in Celsius = " + temperature);
}
}

Let’s work out the program line by line

Step-by-Step Explanation on How to convert Fahrenheit to Celsius Program in Java

1. import java.util.*;

It imports the package java.util containing utility classes such as Scanner. The class Scanner is used for getting the input from the user.

2. class FahrenheitToCelsius

It defines a class called FahrenheitToCelsius. This class includes the conversion of temperature from Fahrenheit to Celsius in it.

3. public static void main(String[] args)

This is the main method, which serves as the entry point for the program.

  • public: The method is declared public, meaning it may be called by any class, and is required to exist so that the JVM can run the program.
  • static: This means that this method can be called without having an instance of this class created.
  •  void: This function does not return any value.
  • String[] args: An array of command-line arguments. Although this program does not make use of this variable, the Java language does require it to exist as part of the main method declaration.

 4. float temperature;

This declares a variable temperature of type float. This variable will hold the value the user enters to represent temperature as well as the result of converting to Celsius.

 5. Scanner in = new Scanner(System.in);

This line declares a Scanner object named in that will read input from the user. The System.in stream reads input from the console (keyboard).

6. System.out.println(“Enter temperature in Fahrenheit”);

This statement prints the message  “Enter temperature in Fahrenheit” to the console, which will prompt the user to enter a temperature value in Fahrenheit.

7. temperature = in.nextInt();

This line reads an integer value input by the user (in Fahrenheit) and assigns it to the variable temperature. The nextInt() method reads the next integer input by the user.

8. temperature = ((temperature – 32) * 5) / 9;

This line converts the temperature from Fahrenheit to Celsius using the formula:

 \[\text{Celsius} = \left(\text{Fahrenheit} – 32\right) \times \frac{5}{9}\]

  • temperature – 32: Subtract 32 from the value in Fahrenheit
  •  * 5: Multiply the result by 5
  •  / 9: Divide the result by 9 to finish the conversion

The output of this calculation is assigned back to the variable temperature so that the temperature is printed in Celsius

 9. System.out.println(“Temperature in Celsius = ” + temperature);

This statement prints the converted temperature in Celsius to the console. It concatenates the string “Temperature in Celsius = ” with the value of the temperature variable, which is now in Celsius.

10. Closing curly braces }

  • The first closing curly brace marks the end of the main method.
  • The second closing curly brace marks the end of the FahrenheitToCelsius class.

Example of Program Execution:

Suppose the user enters the following Fahrenheit input:

Fahrenheit Input: 100

Execution Explanation:

1. The program asks the user to input a temperature in Fahrenheit.

2. The user inputs 100.

3. The formula is applied as follows to convert the input:

  \[\begin{array}{rcl}

 {\rm Celsius} & = & \left(100 – 32\right) \times \frac{5}{9} = 68 \times \frac{5}{9} = 37.777. \approx 37.78

 \end{array}\]

4. The program outputs

Temperature in Celsius = 37.78

Output:

For an input of 100 Fahrenheit, the output will be:

Enter temperature in Fahrenheit
100

Temperature in Celsius = 37.77778

(Note: Depending on the system and how Java handles floating-point numbers, the result may be displayed with a different number of decimal places, but the value will remain the same.)

Leave a Comment

Your email address will not be published. Required fields are marked *