Java Program to Convert Fahrenheit to Celsius

In this tutorial, you will learn how to write a Java program to convert Fahrenheit to Celsius. We will break this down into three steps:

  • Understanding the formula used for temperature conversion.
  • Writing a basic program using a predefined Fahrenheit value.
  • Making it interactive by accepting user input via the Scanner class.

Fahrenheit to Celsius Conversion

Fahrenheit and Celsius are two commonly used temperature scales. To convert a temperature from Fahrenheit to Celsius, we use the following formula:

Celsius = (Fahrenheit − 32) × 5 / 9

Where:

  • Fahrenheit represents the temperature in Fahrenheit.
  • Celsius represents the converted temperature in Celsius.
  • 32 is subtracted as part of the Fahrenheit-to-Celsius conversion formula.
  • 5/9 is the conversion factor between the two temperature scales.

Example:

Let’s consider an example where the temperature is 98.6°F. We can convert it into Celsius using the formula:

Celsius = (98.6 − 32) × 5 / 9
= 66.6 × 5 / 9
= 37°C

Therefore, 98.6°F is equal to 37°C.

Java Program to Convert Fahrenheit to Celsius

Problem Statement: Write a Java program to convert a temperature from Fahrenheit to Celsius by assigning a predefined value directly to a variable.

Complete Java Program

/**
 * Java Program to Convert Fahrenheit to Celsius
 */
public class FahrenheitToCelsius {
    public static void main(String[] args) {
        // Declare and initialize the temperature in Fahrenheit using a double
        // to accurately handle decimal values (e.g., 98.6°F).
        double fahrenheit = 98.6;

        // Convert Fahrenheit to Celsius using the standard formula: C = (F - 32) * (5 / 9).
        // Note: Using 5.0 / 9.0 ensures floating-point division and avoids integer truncation.
        double celsius = (fahrenheit - 32) * 5.0 / 9.0;

        // Display the converted temperatures on the console.
        System.out.println("Temperature in Fahrenheit: " + fahrenheit + "°F");
        System.out.println("Temperature in Celsius: " + celsius + "°C");
    }
}

Expected Output:

Temperature in Fahrenheit: 98.6°F
Temperature in Celsius: 37.0°C

In this Java program:

  • We have created a public class named FahrenheitToCelsius, which encapsulates the program.
  • Then, we have declared a main method, which serves as the entry point of the Java application. The main() method is the starting point of the program execution.
  • After that, we declared a variable named fahrenheit of type double to store the temperature in Fahrenheit. The double data type is used to accurately handle fractional (decimal) values.
  • The expression (fahrenheit – 32) * 5 / 9 converts Fahrenheit into Celsius and stores the resulting temperature in the celsius variable.
  • The statement System.out.println() displays the Fahrenheit and Celsius temperatures on the console.

The most important statement in the program is:

double celsius = (fahrenheit - 32) * 5 / 9;

This statement applies the Fahrenheit-to-Celsius conversion formula and stores the result in the celsius variable.

Java Program to Convert Fahrenheit to Celsius Using Scanner

Problem Statement: Write an interactive Java program to accept a temperature in Fahrenheit from the user using the Scanner class, pass it to a user-defined instance method to perform the calculation, and display the converted Celsius temperature.

Complete Java Program

/**
 * Java Program to Convert Fahrenheit to Celsius using Scanner class.
 */
import java.util.Scanner;

public class FahrenheitToCelsiusConverter {
    // User-defined instance method to convert Fahrenheit to Celsius
    public double convertToCelsius(double fahrenheit) {
        return (fahrenheit - 32) * 5 / 9;
    }

    public static void main(String[] args) {
        // Create a Scanner object to read input from the user
        Scanner scanner = new Scanner(System.in);

        // Prompt the user to enter the temperature in Fahrenheit
        System.out.print("Enter temperature in Fahrenheit: ");
        double fahrenheit = scanner.nextDouble();

        // Create an instance of the class to call the instance method
        FahrenheitToCelsiusConverter converter = new FahrenheitToCelsiusConverter();

        // Call the user-defined instance method
        double celsius = converter.convertToCelsius(fahrenheit);

        // Display the results
        System.out.println("Temperature in Fahrenheit: " + fahrenheit + "°F");
        System.out.println("Temperature in Celsius: " + celsius + "°C");

        // Close the scanner resource to prevent memory leaks
        scanner.close();
    }
}

Expected Output:

Enter temperature in Fahrenheit: 98.6 
Temperature in Fahrenheit: 98.6°F 
Temperature in Celsius: 37.0°C

In this Java program:

  • The statement import java.util.Scanner; imports the Scanner class for reading input from the keyboard.
  • A Scanner object is created to accept the Fahrenheit temperature from the user.
  • The convertToCelsius() user-defined instance method accepts the Fahrenheit temperature as a parameter.
  • The method applies the formula (fahrenheit – 32) * 5 / 9 and returns the calculated Celsius value.
  • An object of the FahrenheitToCelsiusConverter class is created to call the instance method.
  • The nextDouble() method reads a decimal or numeric value entered by the user.
  • The converted Celsius temperature is stored in the celsius variable.
  • System.out.println() displays the Fahrenheit and Celsius temperatures.
  • scanner.close() closes the Scanner resource to free up memory.

Key Points to Remember

  • The Fahrenheit-to-Celsius formula is Celsius = (Fahrenheit − 32) × 5 / 9.
  • Use the double data type when the temperature can contain decimal values.
  • The Scanner class can be used to accept temperature input dynamically from the user.
  • The nextDouble() method reads a numeric or decimal value entered by the user.
DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.