Temperature Converter

In this program, you will learn how you can convert number temperature entered in celsius into kelvin and Fahrenheit

fun main(args: Array<String>) {
    println("Enter a Temprature in Celsius")//1
    val d= readLine()?.toFloat()!!//2
    println("Select Option to Convert\nF for Fahrenheit\nK for Kelvin")//3
    val choice = readLine()//4
    val result = when(choice)//5
    {
        "F","f"->"Temperature in Fahrenheit=${(d*9/5)+32}" //(0.5°C × 9/5) + 32 = 32.9°F//6
        "K","k"->"Temperature in Kelvin=${(d+273.15)}"//0.5°C + 273.15 = 273.65K//7
        else-> "Sorry wrong choice"//8
    }
    println(result)//9
}
  1. Instruct the user to enter the temperature in celsius
  2. Convert the Entered value into float and assigned to a variable d
  3. Select Conversion choice
  4. Assign the user entered choice to choice variable
  5. Assign the result of the calculation to result variable
  6. If user select conversion in Fahrenheit the result return after conversion
  7. If user select conversion in Kelvin the result return after conversion
  8. If the user enters any other value for choice the execute else part
  9. Show the result to the user on command prompt

Output

Enter a Temperature in Celsius
10
Select Option to Convert
F for Ferniheat
K for Kelvin
F
Temperature in Ferniheat=50.0

Enter a Temperature in Celsius
4
Select Option to Convert
F for Fahrenheit
K for Kelvin
k
Temperature in Kelvin=277.15

Enter a Temperature in Celsius
10
Select Option to Convert
F for Fahrenheit
K for Kelvin
l
Sorry wrong choice

Leave a Reply

Your email address will not be published. Required fields are marked *