Program to Convert Fahrenheit into Celsius
Temperature is often measured in different units, and Fahrenheit and Celsius are two commonly used scales. Converting temperatures between these scales is a common task in programming. In this article, we will explore the logic and methods to convert Fahrenheit into Celsius using a programming language.
Formula To Convert Fahrenheit Into Celsius
The Fahrenheit to Celsius conversion formula is:
[Tex]Fahrenheit = \frac{(Celsius \times 9)}{5} + 32 [/Tex]
This formula is based on the relationship between the Fahrenheit and Celsius scales. To convert a temperature from Fahrenheit to Celsius, subtract 32 from the Fahrenheit temperature, then multiply the result by 5/9.
Program To Convert Fahrenheit Into Celsius
Below, are the methods Program To Convert Fahrenheit Into Celsius in Python.
- Direct Calculation
- Using Lambda Function
- Using a Conversion Class
Convert Fahrenheit Into Celsius Using Direct Calculation
In this example, the code defines a function `farenheit_to_celcius` that converts a given Fahrenheit temperature to Celsius and prints the result. It then sets a Fahrenheit temperature to 40 and calls the function, displaying the converted Celsius temperature.
Python3
def farenheit_to_celcius(num): celcius = (farenheit - 32 ) * 5 / 9 print ( "Temperature in Celsius:" , round (celcius, 3 )) farenheit = 40 farenheit_to_celcius(farenheit) |
Output
Temperature in Celsius: 4.444
Convert Fahrenheit Into Celsius Using Lambda Function
In this example, below code utilizes a lambda function to convert Fahrenheit to Celsius. It assigns the lambda function to `fahrenheit_to_celsius`, which takes a Fahrenheit temperature as input and returns the corresponding Celsius value. The code then sets a Fahrenheit temperature to 40, calculates and stores its Celsius equivalent.
Python3
# using lambda function fahrenheit_to_celsius = lambda fahrenheit: (fahrenheit - 32 ) * 0.5556 fahrenheit = 40 celsius = fahrenheit_to_celsius(fahrenheit) print ( "Temperature in Celsius:" , celsius) |
Output
Temperature in Celsius: 4.4448
Convert Fahrenheit Into Celsius Using a Conversion Class
In this example, below code defines a `TemperatureConverter` class with an `__init__` method to initialize the Fahrenheit temperature. The class has a `convert_to_celsius` method, which calculates and returns the Celsius equivalent. In the example usage, a temperature of 75.5 Fahrenheit is converted using an instance of the class.
Python3
class TemperatureConverter: def __init__( self , fahrenheit): self .fahrenheit = fahrenheit def convert_to_celsius( self ): celsius = ( self .fahrenheit - 32 ) * 5 / 9 return celsius # Example Usage temperature_f = 75.5 converter = TemperatureConverter(temperature_f) result = converter.convert_to_celsius() print (f "{temperature_f} degrees Fahrenheit is equal to {result:.2f} degrees Celsius." ) |
Output
75.5 degrees Fahrenheit is equal to 24.17 degrees Celsius.
Conclusion
Creating a program to convert Fahrenheit to Celsius in Python is a simple yet useful task. The conversion formula is straightforward and can be implemented using various methods. In this article, we demonstrated three different approaches: direct calculation, a function with user input, and a class-based implementation. Choose the method that best suits your application or coding style.
Contact Us