Task
In this challenge, you were provided with a variable that stored the temperature in degrees Fahrenheit and you were asked to convert it to degrees Celsius.
Solution
The formula used for converting temperatures from degrees Fahrenheit to Celsius is:
(fahrenheit - 32) * (5D / 9D)
All you have to do is rewrite celsius
and assign it the conversion formula above.
val celsius = (fahrenheit - 32) * (5D / 9D)
You can find the complete solution below:
You were required to write the code on line 2.
val fahrenheit: Double = 50val celsius = (fahrenheit - 32) * (5D / 9D)// Driver Codeprintln(celsius)
This challenge was to teach you about operator precedence and how you should use it to write your program.
You also had to be careful of the data types of the numbers, as 5 / 9 results in a value of type Double
, hence, 5 and 9 needed to be of type Double
as well.
Let’s wrap up this chapter with a quiz to test what you have learned so far.