Progress

Python Program to convert temperature to and from Celsius Fahrenheit

Python Program to convert temperature to and from Celsius Fahrenheit

Celsius and Fahrenheit are two common temperature measures. It is a general problem to convert them to each other. Here is an optimized Python program to convert temperature from Celcius to Fahrenheit and Fahrenheit to Celsius.

The formula to convert temperature from Celsius to Fahrenheit is F = (C × 9/5) + 32.

The formula to convert temperature from Fahrenheit to Celsius is C = (F − 32) × 5/9.

Now we will implement them in Python program to convert temperature from Celsius to Fahrenheit.

Python program: Convert Temperature from Celsius to Fahrenheit

Copy
ctemp = float(input("Enter Temperature in Celsius: "))
ftemp = (ctemp*(9/5))+32
print("{0}°C is equivalent to {1} Fahrenheit".format(ctemp, ftemp))
Output
Enter Temperature in Celsius: 32
32.0°C is equivalent to 89.6 Fahrenheit

In this Python source code, we take input and convert it to float type to preserve decimal points then we convert the temperature from Celsius to Fahrenheit using the formula.

At last, we print the results using the string format method.

Python program: Convert Temperature from Fahrenheit to Celsius

Copy
ftemp = float(input("Enter Temperature in Fahrenheit: "))
ctemp = (ftemp-32)*(5/9)
print("{0} F is equivalent to {1} Celsius".format(ftemp, ctemp))
Output
Enter Temperature in Fahrenheit: 89.6
89.6 F is equivalent to 32.0 Celsius

This program works the same way as above but the formula is different to convert temperature from Fahrenheit to Celsius.

Related Python Examples:

Share the Post ;)

Related Posts