Open In App

Convert Celsius To Fahrenheit - Python

Last Updated : 26 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We are given the temperature in Celsius and our task is to convert the temperature value in the Fahrenheit 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. For example, given temperature is 40 Celsius then after conversion it should be 104.00 Fahrenheit.

Using a Fixed Value for Celsius

In this approach, we assume a fixed Celsius value and perform the conversion to Fahrenheit using the formula:

F = (C * 9/5) + 32

We have used "%.2f" to represent the floating point number up to two decimal places in Python.

Python
c = 47

# Converting the temperature to Fahrenheit using the formula
f = (c * 1.8) + 32

print(str(c))
print(str(f))

Output
47
116.60000000000001

Explanation:

  • The celsius value is set to 47 degrees.
  • The formula fahrenheit = (celsius * 1.8) + 32 is used to calculate the corresponding temperature in Fahrenheit.

User Input for Celsius

In this approach, the program prompts the user to input a temperature in Celsius, then converts it to Fahrenheit and displays the result.

Python
c = float(input("Enter temperature in celsius: "))

f = (c * 1.8) + 32

print(str(c))
print(str(f))

Output:

40 
104.0

Explanation:

  • The program prompts the user to input a temperature in Celsius using input(). The float() function is used to ensure that the input is treated as a numeric value.
  • The conversion formula is then applied to calculate the temperature in Fahrenheit.

Convert Fahrenheit to Celsius

Using a Fixed Fahrenheit Value

To convert Celsius to Fahrenheit we are going to use the formula:

C = (F - 32)/1.8.

Python
f = 104

# Converting the temperature to Celsius
c = (f - 32) / 1.8

print(str(f))
print(str(c))

Output
104
40.0

Explanation: Converts a predefined Fahrenheit value to Celsius using the formula Celsius = (Fahrenheit - 32) / 1.8.

Using User Input for Fahrenheit

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.

Python
f = float(input("Enter temperature in fahrenheit: "))

c = (f - 32)/1.8

print(str(f))
print(str(c))

Output:

140
60.0

Explanation:

  • Takes Fahrenheit input from the user and converts it to Celsius using the same formula.
  • Outputs the user-provided Fahrenheit value and its corresponding Celsius value.

Next Article

Similar Reads