0
0
JavaProgramBeginner · 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, divide by 5, then add 32. This formula changes the scale from Celsius to Fahrenheit, which have different zero points and increments.
📐

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(celsius + " Celsius is " + fahrenheit + " Fahrenheit");
    }
}
Output
25.0 Celsius is 77.0 Fahrenheit
🔍

Dry Run

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

1

Input Celsius

celsius = 25

2

Multiply by 9

25 * 9.0 = 225.0

3

Divide by 5

225.0 / 5.0 = 45.0

4

Add 32

45.0 + 32 = 77.0

5

Output Fahrenheit

fahrenheit = 77.0

StepOperationResult
1Input Celsius25
2Multiply by 9225.0
3Divide by 545.0
4Add 3277.0
5Output Fahrenheit77.0
💡

Why This Works

Step 1: Multiply Celsius by 9/5

The formula fahrenheit = (celsius * 9/5) + 32 converts the scale size from Celsius to Fahrenheit by multiplying by 9/5.

Step 2: Add 32 to adjust zero point

Adding 32 shifts the zero point from Celsius to Fahrenheit, because 0°C equals 32°F.

🔄

Alternative Approaches

Using integer arithmetic
java
public class CelsiusToFahrenheit {
    public static void main(String[] args) {
        int celsius = 25;
        int fahrenheit = (celsius * 9 / 5) + 32;
        System.out.println(celsius + " Celsius is " + fahrenheit + " Fahrenheit");
    }
}
This uses integer math which can lose decimal precision but is simpler for whole numbers.
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) {
        double celsius = 25;
        System.out.println(celsius + " Celsius is " + convert(celsius) + " Fahrenheit");
    }
}
Separates conversion logic into a reusable method for cleaner code.

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 space usage is constant.

Which Approach is Fastest?

All approaches run in constant time; using integer arithmetic is slightly faster but less precise.

ApproachTimeSpaceBest For
Double arithmeticO(1)O(1)Accurate decimal conversion
Integer arithmeticO(1)O(1)Simple whole number conversion
Method-based conversionO(1)O(1)Reusable and clean code
💡
Use double type for Celsius and Fahrenheit to handle decimal temperatures accurately.
⚠️
Forgetting to use 9.0 and 5.0 as doubles causes integer division and wrong results.