Python Program to Convert Celsius to Fahrenheit and Vice Versa using Functions

We will develop a Python program to convert Celsius to Fahrenheit and vice versa using functions. 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 = 15
Fahrenheit = (15 * 1.8) + 32 = 59
15 degrees Celsius is equivalent to 59 degrees Fahrenheit

Python Program to Convert Celsius to Fahrenheit using Function

We can take the help of a function to convert temperature Celsius to Fahrenheit in python. A function is a block of code that performs a specific task. We will take a value of temperature in Celsius when declaring the variables. Then, call the function and finally, the result will be displayed on the screen.

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 = 15

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

Output:-

15.0 degrees Celsius is equivalent to 59.0 degrees Fahrenheit

Convert Celsius to Fahrenheit in Python using Function

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

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

# 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: 32
32.0 degrees Celsius is equivalent to 89.6 degrees Fahrenheit

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

Enter temperature in Celsius: -15
-15.0 degrees Celsius is equivalent to 5.0 degrees Fahrenheit

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

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

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.

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

# 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: 45
45.0 degrees Fahrenheit is equivalent to 7.2 degrees Celsius

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

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

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

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

Python Program to Convert Celsius to Fahrenheit and Vice Versa using Functions

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 to convert Celsius to Fahrenheit and vice versa using functions

# 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: 32
32.0 degrees Celsius is equivalent to 89.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: 56.1
56.1 degrees Fahrenheit is equivalent to 13.4 degrees Celsius

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

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

Leave a Comment

Your email address will not be published.