Python Program that Converts Temperature in Celsius to Fahrenheit and Vice Versa

We will develop a Python program that converts temperature in Celsius to Fahrenheit and vice versa. Conversion from Celsius to Fahrenheit and from Fahrenheit to Celsius has an important role in the conversion of units system. Celsius is also known as centigrade. It is an SI-derived unit used by most countries worldwide.

Celsius to Fahrenheit Formula is given as,
⁰F= (⁰C * 9/5) + 32 or ⁰F= (⁰C * 1.8) + 32

Mathematically,

Celsius = 10
Fahrenheit = (10 * 1.8) + 32 = 50
10 degrees Celsius is equivalent to 50 degrees Fahrenheit

Python Program to Convert Celsius to Fahrenheit

We will take a value of temperature in Celsius when declaring the variables. Then, find the value of temperature in Fahrenheit and store it in the variable. Finally, it will be displayed on the screen.

Program description:- Write a program that converts Celsius temperatures to Fahrenheit temperatures

# Python program to convert Celsius to Fahrenheit

# take inputs
cel = 10

# find temprature in Fahrenheit
fahr = (cel * 1.8) + 32

# print temperature in Fahrenheit
print('%0.1f degrees Celsius is equivalent to %0.1f 
                       degrees Fahrenheit' %(cel, fahr))

Output:-

10.0 degrees Celsius is equivalent to 50.0 degrees Fahrenheit

Convert Celsius to Fahrenheit in Python

In the previous program, input is hardcoded in the program but in this program, input will be provided by the user.

# Python program to convert Celsius to Fahrenheit

# take inputs
cel = float(input('Enter temperature in Celsius: '))

# find temperature in Fahrenheit
fahr = (cel * 1.8) + 32

# print temprature in Fahrenheit
print('%0.1f degrees Celsius is equivalent to %0.1f 
                       degrees Fahrenheit' %(cel, fahr))

Output for the input values test-case-1:-

Enter temperature in Celsius: 35
35.0 degrees Celsius is equivalent to 95.0 degrees Fahrenheit

Output for the input values test-case-2:-

Enter temperature in Celsius: -45
-45.0 degrees Celsius is equivalent to -49.0 degrees Fahrenheit

Celsius to Fahrenheit in Python using Function

We can also take the help of a function to convert temperature Celsius to Fahrenheit. A function is a block of code that performs a specific task.

Program description:- Write a Python program using the function to convert Celsius to Fahrenheit

# Python program to convert Celsius to Fahrenheit using function

def convertTemp(c):  #user-defined function
   # find temperature in Fahrenheit
   f = (c * 1.8) + 32
   return f
    
# take inputs
cel = float(input('Enter temperature in Celsius: '))

# calling function and display result
fahr = convertTemp(cel)
print('%0.1f degrees Celsius is equivalent to %0.1f 
                       degrees Fahrenheit' %(cel, fahr))

Output for the input values test-case-1:-

Enter temperature in Celsius: 12.3
12.3 degrees Celsius is equivalent to 54.1 degrees Fahrenheit

Output for the input values test-case-2:-

Enter temperature in Celsius: 100
100.0 degrees Celsius is equivalent to 212.0 degrees Fahrenheit

Python Program to Convert Fahrenheit to Celsius

In the previous program, we converted Celsius to Fahrenheit but in this program, we convert Fahrenheit to Celsius.

Fahrenheit to Celsius Formula is given as,
⁰C = (5/9) * (⁰F–32) or ⁰C = (⁰F–32) / 1.8

Mathematically,

Fahrenheit = 50
Celsius = (50-32) / 1.8 = 10
50 degrees Fahrenheit is equivalent to 10 degrees Celsius

Program description:- Write a python program to convert temperatures to and from Fahrenheit Celsius

# Python program to convert Fahrenheit to Celsius

# take inputs
fahr = float(input('Enter temperature in Fahrenheit: '))

# find temperature in Celsius
cel = (fahr-32) / 1.8

# print temperature in Celsius
print('%0.1f degrees Fahrenheit is equivalent to %0.1f 
                            degrees Celsius' %(fahr, cel))

Output for the input values test-case-1:-

Enter temperature in Fahrenheit: 50
50.0 degrees Fahrenheit is equivalent to 10.0 degrees Celsius

Output for the input values test-case-2:-

Enter temperature in Fahrenheit: 75.03
75.0 degrees Fahrenheit is equivalent to 23.9 degrees Celsius

Convert Fahrenheit to Celsius in Python using Function

We can also take the help of a function to convert temperature Fahrenheit to Celsius. A function is a block of code that performs a specific task.

# Python program to convert Fahrenheit to Celsius using function

def convertTemp(f):  #user-defined function
   # find temperature in Celsius
   c = (f-32) / 1.8
   return c

# take inputs
fahr = float(input('Enter temperature in Fahrenheit: '))

# calling function and display result
cel = convertTemp(fahr)
print('%0.1f degrees Fahrenheit is equivalent to %0.1f 
                            degrees Celsius' %(fahr, cel))

Output for the input values test-case-1:-

Enter temperature in Fahrenheit: -25
-25.0 degrees Fahrenheit is equivalent to -31.7 degrees Celsius

Output for the input values test-case-2:-

Enter temperature in Fahrenheit: 100
100.0 degrees Fahrenheit is equivalent to 37.8 degrees Celsius

Python Program that Converts Temperature in Celsius to Fahrenheit and Vice-Versa

This program performs the same task but in different ways. In this program, we convert temperature Celsius to Fahrenheit and Fahrenheit to Celsius in one source code.

Program description:- Write a Python program that converts temperature in Celsius to Fahrenheit and vice versa using function

# Python program to convert Celsius to Fahrenheit and vice-versa

# find temperature in Fahrenheit
def convertFahr(c):  #user-defined function
   f = (c * 1.8) + 32
   return f

# find temperature in Celsius
def convertCel(f):  #user-defined function
   c = (f-32) / 1.8
   return c

# select operation
print("Operation: C to F, F to C")
select = input("Select operations: ")

if select == "C to F":
   # take inputs
   cel = float(input('Enter temperature in Celsius: '))
    
   # calling function and display result
   print('%0.1f degrees Celsius is equivalent to %0.1f 
                 degrees Fahrenheit' %(cel, convertFahr(cel)))

elif select == "F to C":
   # take inputs
   fahr = float(input('Enter temperature in Fahrenheit: '))
   
   # calling function and display result
   print('%0.1f degrees Fahrenheit is equivalent to %0.1f 
                    degrees Celsius' %(fahr, convertCel(fahr)))

else:
   print("Invalid selection")

Output for the input values test-case-1:-

Operation: C to F, F to C
Select operations: C to F
Enter temperature in Celsius: 22
22.0 degrees Celsius is equivalent to 71.6 degrees Fahrenheit

Output for the input values test-case-2:-

Operation: C to F, F to C
Select operations: F to C
Enter temperature in Fahrenheit: 42.9
42.9 degrees Fahrenheit is equivalent to 6.1 degrees Celsius

Output for the input values test-case-3:-

Operation: C to F, F to C
Select operations: C to K
Invalid selection

Leave a Comment

Your email address will not be published.