Python Program to Convert Celsius to Fahrenheit using Function

We will develop a Python program to convert Celsius to Fahrenheit using function. 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 = 25
Fahrenheit = (25 * 1.8) + 32 = 77
25 degrees Celsius is equivalent to 77 degrees Fahrenheit

Convert Celsius to Fahrenheit in Python 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 while declaring the variables. Then, call the function and finally, the value of temperature in Fahrenheit 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 = 25

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

Output:-

25.0 degrees Celsius is equivalent to 77.0 degrees Fahrenheit

Python Program to Convert Celsius to Fahrenheit 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: 35
35.0 degrees Celsius is equivalent to 95.0 degrees Fahrenheit

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

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

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

Enter temperature in Celsius: 83.25
83.2 degrees Celsius is equivalent to 181.8 degrees Fahrenheit

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

Enter temperature in Celsius: 125.06
125.1 degrees Celsius is equivalent to 257.1 degrees Fahrenheit

Leave a Comment

Your email address will not be published. Required fields are marked *