Ad space (banner)
#๏ธโƒฃC# Lessons
Lesson 66 / 68

Converting Temperature (Celsius to Fahrenheit)

In this lesson you will learn how to convert between Celsius and Fahrenheit in C#, practising how to turn a formula into code. This is for anyone searching for "C# 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 simple but genuinely useful practice at translating coefficients and constants into code. 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 C# 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)