Python Program to convert Celsius to Fahrenheit
Program
# Python Program to convert temperature in celsius to fahrenheit
celsiusVal = float(input('Enter a Celsius value you want to convert: '))
# calculate fahrenheit using the formula
# celsius * 1.8 = fahrenheit - 32
fahrenheit = (celsiusVal * 1.8) + 32
print('%0.1f degree Celsius = %0.1f degree Fahrenheit' %(celsiusVal,fahrenheit))
This program converts a temperature given in Celsius to Fahrenheit using a mathematical formula.
-
Take User Input
- The user enters a temperature in Celsius as a floating-point number.
-
Convert to Fahrenheit
- The program applies the formula:
[
\text{Fahrenheit} = (\text{Celsius} \times 1.8) + 32 ] - This formula converts Celsius to Fahrenheit.
- The program applies the formula:
-
Display Output
- The result is printed with one decimal place using
'%0.1f'
.
- The result is printed with one decimal place using
Key Takeaways
- The formula (C × 1.8) + 32 is used for conversion.
- The program supports decimal values as input.
- The output is formatted to one decimal place for better readability.
Output
$ python3 ConvertCelsiusToFahrenheit.py
Enter a Celsius value you want to convert: 37.5
37.5 degree Celsius = 99.5 degree Fahrenheit