Temperature Conversion
You are given the temperature T of an object in one of Celsius, Fahrenheit, and Kelvin scales. Write a program to print T in all scales viz Celsius, Fahrenheit, and Kelvin. Formula to convert from Fahrenheit F to Celsius C is C = (F - 32) * 5 / 9.Formula to convert from Kelvin K to Celsius C is C = K - 273.Here "C", "F", "K" represent that the temperature scale is in Celsius, Fahrenheit and Kelvin scales respectively. The input contains the temperature (a number) and the unit of the temperature scale (C,F, K) without any space. The output contains temperature in Celsius, Fahrenheit and Kelvin scales in each line in the format similar to input and the value of the temperature is rounded to 2 decimal places.
t = input()
scale = t[-1]
t = float(t[:-1])
if scale == 'C':
print(f'{round(t, 2)}C')
print(f'{round(t * 9 / 5 + 32, 2)}F')
print(f'{round(t + 273, 2)}K')
elif scale == 'K':
print(f'{round(t - 273, 2)}C')
print(f'{round((t - 273) * 9 / 5 + 32, 2)}F')
print(f'{round(t, 2)}K')
elif scale == 'F':
print(f'{round((t - 32) * 5 / 9, 2)}C')
print(f'{round(t, 2)}F')
print(f'{round((t - 32) * 5 / 9 + 273, 2)}K')
Comments
Leave a comment