Problem: Write a program in Python to convert Celsius into Fahrenheit and vice versa.

First, we need to take input the celsius and use the following formula to convert it into Fahrenheit.

fahrenheit = (9/5)*celsius + 32

We can use the same formula to convert the Celcius to Fahrenheit in Python.

#input celcius
celsius = float(input("Enter Celcius: "))

#calculate fahrenheit using the formula
fahrenheit = (9/5)*celsius + 32
print("Fahrenheit ",fahrenheit)

Output:

Enter Celcius: 37.5
Fahrenheit 99.5

If we move all the variables to the left-hand side, we will get the formula to convert Fahrenheit back to Celsius:

celsius = (5/9)*(fahrenheit - 32)

Following is the python source code to convert Fahrenheit to Celsius.

#input fahrenheit
fahrenheit = float(input("Enter Fahrenheit: "))

celsius = (5/9)*(fahrenheit - 32)
print("Celsius ",celsius)

Output:

Enter Fahrenheit: 99.5
Celsius 37.5

Leave a Reply