How to Convert Celsius to Fahrenheit in Java
The Challenge
Convert temperature from Celsius to Fahrenheit using the formula: F = (C × 9/5) + 32
Solution
public class Main {
public static void main(String[] args) {
double celsius = 25.0;
double fahrenheit = (celsius * 9/5) + 32;
System.out.println(celsius + "°C = " + fahrenheit + "°F");
// Output: 25.0°C = 77.0°F
}
}Fahrenheit to Celsius
double fahrenheit = 77.0;
double celsius = (fahrenheit - 32) * 5/9;
System.out.println(fahrenheit + "°F = " + celsius + "°C");
// Output: 77.0°F = 25.0°CUsing Methods
public static double celsiusToFahrenheit(double celsius) {
return (celsius * 9/5) + 32;
}
public static double fahrenheitToCelsius(double fahrenheit) {
return (fahrenheit - 32) * 5/9;
}
// Usage
System.out.println(celsiusToFahrenheit(0)); // 32.0
System.out.println(celsiusToFahrenheit(100)); // 212.0
System.out.println(fahrenheitToCelsius(32)); // 0.0
System.out.println(fahrenheitToCelsius(212)); // 100.0Did You Know?
The Celsius and Fahrenheit scales intersect at -40°! That means -40°C = -40°F. It's the only temperature where both scales show the same number!
Watch Out!
Use 9.0/5.0 instead of 9/5 to avoid integer division:
// Wrong - integer division
double f = (25 * 9/5) + 32; // 77.0 (works but risky)
// Correct - floating-point division
double f = (25 * 9.0/5.0) + 32; // 77.0 (safer)Real-World Use Cases
- Weather apps: Display temperature in user's preferred unit
- Cooking: Convert recipe temperatures
- Science: Convert between temperature scales
- Travel: Understand local weather forecasts
›Short versionOpen if you only have a minute.
- The formula is F = C * 9 / 5 + 32. The division has to see a double, or the fraction disappears.
- 9 / 5 in ints is 1. 1 * 9 / 5 in ints is 1, then + 32 is 33, not 33.8.
- 25 hides the bug: 25 * 9 is divisible by 5, so the int path still prints 77.
- Format the number when you print it. Do not round it before the formula finishes.
Celsius to Fahrenheit
Convert Celsius to Fahrenheit using F = C * 9 / 5 + 32.
Working approach
- Keep the temperature in a double.
- Write celsius * 9.0 / 5.0 + 32 so neither factor forces integer division.
- Apply the inverse, (fahrenheit - 32) * 5.0 / 9.0, with the same rule.
Walk the sample
- Input 25 C with double arithmetic
- 25 * 9 / 5 is 45
- 45 + 32 is 77 F
The trap
Left-to-right integer arithmetic truncates. The sample input 25 still prints 77, so the bug shows up on values such as 1.
static double toFahrenheit(double celsius) {
return celsius * 9.0 / 5.0 + 32;
}
// int path: 1 * 9 / 5 + 32 == 33, not 33.8
// 25 * 9 / 5 + 32 == 77 only because 225 is divisible by 5What does int f = 1 * 9 / 5 + 32 produce, and why does 25 still look correct?
1 * 9 / 5 is 9 / 5, which is 1 in integer arithmetic, then + 32 is 33.
25 * 9 is 225, and 225 / 5 is 45 with no remainder, so the truncated formula happens to match 77. The test case in the sample does not expose the bug.
Time O(1), space O(1).
Easy to get wrong
- Writing 9 / 5 with two int literals.
- Trusting the result for 25 and not trying a value that is not a multiple of 5.