Python Program for Fahrenheit to Celsius conversion



Problem Description

In this problem, we are given a temperature value in Fahrenheit and we have to convert it to Celsius. This conversion is used in various real-world applications such as weather applications, scientific calculations, and data analysis. In this article, we are going to discuss, how we can convert Fahrenheit temperature to Celsius temperature in Python.

Formula to Convert Fahrenheit to Celsius

The formula for converting Fahrenheit to Celsius is: Celsius = (Fahrenheit - 32) / 1.8

Example 1

  • Input: Fahrenheit = 98.6
  • Output: Celsius = 37°C

Explanation:

The given temperature is 98.6°F, we calculate:
Celsius = (98.6 - 32) / 1.8
= 37°C

Example 2

  • Input: Fahrenheit = 50
  • Output: Celsius = 10°C

Explanation:

The given temperature is 50°F, we calculate:
Celsius = (50 - 32) / 1.8
= 10°C

Below are different approaches for Fahrenheit to Celsius conversion

Direct Formula Approach

In this approach, we use the direct formula to convert the given Fahrenheit value to Celsius. The formula for converting Fahrenheit to Celsius: Celsius = Fahrenheit - 32 / 1.8 We subtract 32 from the Fahrenheit value and divide the result by 1.8. After the conversion, we print the output.

Steps for Implementation

  1. Take the input temperature in Fahrenheit.
  2. Use the formula to calculate Celsius: Celsius = (Fahrenheit?32) / 1.8
  3. Print the result in Celsius.

Implementation Code

fahrenheit = 100
celsius = (fahrenheit - 32) / 1.8
print(f"{fahrenheit}°F is equal to {celsius}°C.")

Output

100°F is equal to 37.77777777777778°C.

Time Complexity: O(1)
Space Complexity: O(1)

Using Custom Function

In this approach, we use a function. The logic for this approach remains the same. However, we create a reusable function to perform the conversion. Functions make the code more organized and allow it to be reused for different inputs.

Steps for Implementation

  1. Create a function that takes Fahrenheit as the input parameter.
  2. Inside the function, use the formula: Celsius = (Fahrenheit?32) / 1.8
  3. Return the calculated Celsius value.
  4. Call the function with different input values and display the output.

Implementation Code

def convert_fahrenheit_to_celsius(fahrenheit):
    celsius = (fahrenheit - 32) / 1.8
    return celsius

# Test cases
fahrenheit = 98.6
result = convert_fahrenheit_to_celsius(fahrenheit)
print(f"{fahrenheit}°F is {result:.2f}°C")

fahrenheit = 50
result = convert_fahrenheit_to_celsius(fahrenheit)
print(f"{fahrenheit}°F is {result:.2f}°C")

fahrenheit = 32
result = convert_fahrenheit_to_celsius(fahrenheit)
print(f"{fahrenheit}°F is {result:.2f}°C")

fahrenheit = 0
result = convert_fahrenheit_to_celsius(fahrenheit)
print(f"{fahrenheit}°F is {result:.2f}°C")

Output

98.6°F is 37.00°C
50°F is 10.00°C
32°F is 0.00°C
0°F is -17.78°C

Time Complexity: O(1)
Space Complexity: O(1)

Real-Life Applications of Fahrenheit to Celsius Conversion

  • Weather Forecasting
  • Some countries use Celsius and some Fahrenheit for reporting weather. One can easily convert one unit to another.
Updated on: 2025-01-21T18:46:44+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started