Python program to convert temperature from Celsius to Fahrenheit and vice-versa

Learn: How to convert Fahrenheit to Celsius and Celsius to Fahrenheit using Python program. By Ankit Rai Last updated : January 04, 2024

Problem statement

Write a Python program to convert temperature from Celsius to Fahrenheit and vice-versa.

Converting Fahrenheit to Celsius and Celsius to Fahrenheit

For this purpose, you can use mathematical formulas for the conversion of temperature. The relationship between Fahrenheit and Celsius is expressed with the formula, C = (F - 32) × 5/9; where C is the Celsius and F is the Fahrenheit.

  • The formula to convert Celsius (C) to Fahrenheit (F) is: F = (C * 9/5) + 32.
  • The formula to convert Fahrenheit (F) to Celsius (C) is: C = (F - 32) / 1.8.

Python code to convert temperature from Celsius to Fahrenheit and vice-versa

# Define a function to convert
# celsius temperature to Fahrenheit
def Celsius_to_Fahrenheit(c):
    f = (9 / 5) * c + 32
    return f

# Define a function to convert
# Fahrenheit temperature to Celsius
def Fahrenheit_to_Celsius(f):
    c = (5 / 9) * (f - 32)
    return c

# Main Code
if __name__ == "__main__":
    c = 36
    print(c, "degree celsius is equal to:", Celsius_to_Fahrenheit(c), "Fahrenheit")
    f = 98
    print(f, "Fahrenheit is equal to:", Fahrenheit_to_Celsius(f), "degree celsius")

Output

The output of the above example is:

36 degree celsius is equal to: 96.8 Fahrenheit
98 Fahrenheit is equal to: 36.66666666666667 degree celsius

To understand the above program, you should have the basic knowledge of the following Python topics:

Python Basic Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.