Celsius to Fahrenheit in Python

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 SI derived unit used by most of the countries worldwide. In this post, we will develop a Python program to convert Celsius to Fahrenheit.

The formula used to convert from Celsius to Fahrenheit is given as,
⁰F= (⁰C * 9/5) + 32 or ⁰F= (⁰C * 1.8) + 32

Mathematically,

Celsius = 40
Fahrenheit = (40 * 1.8) + 32 = 104
40.0 degrees Celsius is equivalent to 104.0 degrees Fahrenheit

Python Program to Convert Celsius to Fahrenheit

This is the simplest and easiest way to convert Celsius to Fahrenheit program in Python. We will take a value of Celsius when declaring the variables, the value of the Fahrenheit will be calculated and stored in the variable, and 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 = 25

# calculate Fahrenheit
fahr = (cel * 1.8) + 32

# display result
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

In this program, we have hardcoded the value of Celsius in the source code.

cel = 25

Now, calculate the value of Fahrenheit using the formula.

fahr = (cel * 1.8) + 32

Then, the result will be displayed on the screen.

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

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: '))

# calculate Fahrenheit
fahr = (cel * 1.8) + 32

# display result
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: 20
20.0 degrees Celsius is equivalent to 68.0 degrees Fahrenheit

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

Enter temperature in Celsius: 32.6
32.6 degrees Celsius is equivalent to 90.7 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.

# Python program to convert Celsius to Fahrenheit using function

def convertTemp(c):  #user-defined function
   # find temprature 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

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Python Programs

1 thought on “Celsius to Fahrenheit in Python”

Leave a Comment

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