Ad space (banner)
๐ŸŽฏKotlin Lessons
Lesson 66 / 68

Converting Temperature (Celsius to Fahrenheit)

In this lesson you will learn how to convert between Celsius and Fahrenheit in Kotlin, practising how to turn a formula into code. This is for anyone searching for "Kotlin Celsius Fahrenheit conversion".

Celsius to Fahrenheit is "C x 9/5 + 32", and Fahrenheit to Celsius is "(F - 32) x 5/9". Unit conversion is a classic programming exercise.

The sample code provides two functions, celsiusToFahrenheit() and fahrenheitToCelsius(), each following its formula. You can see 25 degrees Celsius convert to 77 Fahrenheit, and 77 Fahrenheit convert back to 25 Celsius.

A common stumbling block for beginners is the order of operations. Get the order of the multiplication, division, and addition in c * 9 / 5 + 32 wrong and the answer is wrong, so understand the formula before turning it into code.

It is a classic conversion exercise: simple formulas turned straight into code, with good practice at keeping types in mind. Special values such as absolute zero (-273.15 C) go through exactly the same formula.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Kotlin 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)