Ad space (banner)
Java Lessons
Lesson 66 / 68

Converting Temperature (Celsius to Fahrenheit)

This lesson covers converting between Celsius and Fahrenheit in Java, so you can practice translating a unit-conversion formula into code. It's for anyone searching "Java Celsius Fahrenheit conversion."

Converting Celsius (°C) to Fahrenheit (°F) uses the formula "°C × 9/5 + 32," and going the other way uses "(°F − 32) × 5/9." Unit conversion is a classic programming exercise.

The example provides two methods, celsiusToFahrenheit() and fahrenheitToCelsius(), each following its respective formula. You can confirm 25°C correctly converts to 77°F, and 77°F correctly converts back to 25°C.

A common early mistake is the order of operations in the formula. Getting the order of multiplication, division, and addition wrong in c * 9 / 5 + 32 produces an incorrect result, so you need to understand the formula's meaning before translating it into code.

It's simple, but genuinely practical practice for translating coefficients and constants straight into code. Even a special value like absolute zero (-273.15°C) can be handled with the exact same formula.

📖 Reference code
✍️ Your code
Type your code, then press "Run"

🧪 This site can't compile or run Java directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser — nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)