In temperature conversions, it is often necessary to convert temperatures between different scales. One common conversion is from Celsius to Fahrenheit and vice-versa. In this article, we will see how to convert Celsius to Fahrenheit and vice-versa.
Input1: 40 Celsius
Output1: 104.00 Fahrenheit
Input2: 140 Fahrenheit
Output2: 60 Celsius
We would be given the temperature in Celsius or Fahrenheit and our task will be to convert the temperature value in the Fahrenheit or Celsius scale and display it.
To convert the temperature from Celsius to Fahrenheit or vice versa in Python firstly we will take the input in Celsius or Fahrenheit and then convert that temperature to another unit using the required formula and then print the output.
Convert Celsius to Fahrenheit using Python
To convert Celsius to Fahrenheit we are going to use the formula “F = (C * 9/5) + 32″. We will take Celsius temperature as input from the user, apply the conversion formula of Fahrenheit from Celsius, and display the result. We have used “%.2f” to represent the floating point number up to two decimal places in Python.
Python3
# Temperature in celsius degree celsius = 47 # Converting the temperature to # fehrenheit using the formula fahrenheit = (celsius * 1.8 ) + 32 # printing the result print ( '%.2f Celsius is equivalent to: %.2f Fahrenheit' % (celsius, fahrenheit)) |
47.00 Celsius is equivalent to: 116.60 Fahrenheit
Taking input from the user to convert Fahrenheit to Celsius in Python:
In the below example, we will simply take temperature input from the user in Celsius and then applied the conversion formula and after that print the result.
Python3
celsius = float ( input ("Enter temperature\ in celsius: ")) fahrenheit = (celsius * 1.8 ) + 32 print ( str (celsius ) + " degree Celsius\ is equal to " + str (fahrenheit ) + " degree Fahrenheit." ) |
Output:
Enter temperaturein celsius: 40
40.0 degree Celsiusis equal to 104.0 degree Fahrenheit.
Python Program to Convert Fahrenheit to Celsius
To convert Celsius to Fahrenheit we are going to use the formula “C = (F – 32)/1.8“. We will take Fahrenheit temperature as input from the user, apply the conversion formula of Celsius from Fahrenheit, and display the result.
Python3
# Temperature in Fahrenheit degree fahrenheit = 104 # Converting the temperature to # fehrenheit celsius = (fahrenheit - 32 ) / 1.8 # printing the result print ( '%.2f Fahrenheit is equivalent to: %.2f Celsius' % (fahrenheit ,celsius)) |
104.00 Fahrenheit is equivalent to: 40.00 Celsius
Taking input from the user to Convert Fahrenheit to Celsius in Python
In the below example, we will simply take temperature input from the user in Fahrenheit and then applied the conversion formula and after that, we print the result.
Python3
fahrenheit = float ( input ( "Enter temperature in fahrenheit: " )) celsius = (fahrenheit - 32 ) / 1.8 print ( str (fahrenheit ) + " degree Fahrenheit is equal\ to " + str(celsius ) + " degree Celsius." ) |
Output:
Enter temperature in fahrenheit: 140
140.0 degree Fahrenheit is equalto 60.0 degree Celsius.