0
0
JavaHow-ToBeginner · 2 min read

Java Program to Convert Celsius to Fahrenheit

To convert Celsius to Fahrenheit in Java, use the formula fahrenheit = (celsius * 9 / 5) + 32. For example, double fahrenheit = (celsius * 9.0 / 5.0) + 32; converts a Celsius value to Fahrenheit.
📋

Examples

Input0
Output32.0
Input25
Output77.0
Input-40
Output-40.0
🧠

How to Think About It

To convert Celsius to Fahrenheit, multiply the Celsius temperature by 9, then divide by 5, and finally add 32. This formula changes the scale from Celsius to Fahrenheit, which uses a different zero point and unit size.
📐

Algorithm

1
Get the Celsius temperature input.
2
Multiply the Celsius value by 9.
3
Divide the result by 5.
4
Add 32 to the result.
5
Return or print the Fahrenheit temperature.
💻

Code

java
public class CelsiusToFahrenheit {
    public static void main(String[] args) {
        double celsius = 25;
        double fahrenheit = (celsius * 9.0 / 5.0) + 32;
        System.out.println("Fahrenheit: " + fahrenheit);
    }
}
Output
Fahrenheit: 77.0
🔍

Dry Run

Let's trace converting 25 Celsius to Fahrenheit through the code

1

Start with Celsius value

celsius = 25

2

Multiply by 9 and divide by 5

(25 * 9) / 5 = 225 / 5 = 45

3

Add 32 to get Fahrenheit

45 + 32 = 77

StepCalculationResult
Multiply by 925 * 9225
Divide by 5225 / 545
Add 3245 + 3277
💡

Why This Works

Step 1: Multiply Celsius by 9

The Celsius temperature is multiplied by 9 to scale it according to the Fahrenheit unit size.

Step 2: Divide by 5

Dividing by 5 adjusts the scale difference between Celsius and Fahrenheit degrees.

Step 3: Add 32

Adding 32 shifts the zero point from Celsius to Fahrenheit, aligning the freezing point of water.

🔄

Alternative Approaches

Using a method to convert
java
public class CelsiusToFahrenheit {
    public static double convert(double celsius) {
        return (celsius * 9.0 / 5.0) + 32;
    }
    public static void main(String[] args) {
        System.out.println("Fahrenheit: " + convert(25));
    }
}
This approach improves code reuse by putting the conversion in a method.
Using integer arithmetic (less precise)
java
public class CelsiusToFahrenheit {
    public static void main(String[] args) {
        int celsius = 25;
        int fahrenheit = (celsius * 9 / 5) + 32;
        System.out.println("Fahrenheit: " + fahrenheit);
    }
}
This uses integers only but loses decimal precision.

Complexity: O(1) time, O(1) space

Time Complexity

The calculation uses a fixed number of arithmetic operations, so it runs in constant time.

Space Complexity

Only a few variables are used, so the space needed is constant.

Which Approach is Fastest?

All approaches run in constant time; using a method adds slight overhead but improves code clarity.

ApproachTimeSpaceBest For
Direct formulaO(1)O(1)Simple quick conversion
Method encapsulationO(1)O(1)Reusable and clean code
Integer arithmeticO(1)O(1)When decimals are not needed
💡
Use double type for Celsius and Fahrenheit to keep decimal precision in your calculations.
⚠️
Forgetting to use 9.0 and 5.0 as doubles causes integer division and wrong results.