Skip to main content

Fahrenheit to Celsius Example in Java

2 min read Updated May 29, 2026
Share:
On this page (9sections)

Introduction

Fahrenheit To Celsius is a classic Java console program that demonstrates the concept with complete source code and sample output. Conversion programs transform values between formats, units or representations.

This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.

Formula

[C] = ([F] - 32) * 5/9

where,
[F] is the temperature in Fahrenheit scale
[C] is the temperature in Celsius scale

Fahrenheit To Celsius Example Program

import java.util.Scanner;

public class FahrenheitToCelsius {
    public static void main(String[] args) {
		System.out.println("Enter a temperature in Fahrenheit: ");
		Scanner in = new Scanner(System.in);
		double Celsius = 0;
		if(in.hasNextDouble()){
			Celsius = (in.nextDouble() - 32)*5/9;
        }
        System.out.println("The temperature in Celsius is: "+Celsius);
    }
 
}

Sample Output

Enter a temperature in Fahrenheit:
183
The temperature in Celsius is: 83.88888888888889

When to use

Use unit conversion programs when reading sensor data, building calculators, or localizing measurements for users.

How it works

  1. Execution begins in the main method — the JVM calls this method when you run the class.

  2. import java.util.Scanner; imports a class used later in the program.

  3. A println / print call writes text to the console — part of the sample output below.

  4. A Scanner reads typed input from the keyboard (System.in).

  5. double Celsius = 0; updates a variable used in the calculation or output.

  6. The if statement runs the nested code only when the condition is true.

  7. Celsius = (in.nextDouble() - 32)*5/9; updates a variable used in the calculation or output.

  8. A println / print call writes text to the console — part of the sample output below.

Best Practices

  • Use double (or BigDecimal for money) to avoid integer division rounding errors.
  • Apply multiplication before addition — match the formula order exactly.
  • Validate input with hasNextDouble() before reading, as shown in the Scanner examples.

Common Mistakes

  • Using integer division (9/5 as 1) instead of floating-point (9.0/5.0).
  • Applying the wrong formula order — (C + 32) * 9/5 is not the same as C * 9/5 + 32.
  • Forgetting to close the Scanner when finished (call in.close() in longer programs).

Frequently Asked Questions

What does the Fahrenheit To Celsius program demonstrate?
It shows how to implement fahrenheit to celsius in Java with a complete runnable example and expected console output.
How do I run this Java program?
Save the code in a `.java` file matching the public class name, compile with `javac`, then run with `java ClassName`.
When would I use this pattern?
Use unit conversion programs when reading sensor data, building calculators, or localizing measurements for users.

Related Tutorials

Search tutorials