Fahrenheit to Celsius Converter in Python
Python Code
# Function to convert Fahrenheit to Celsius
def fahrenheit_to_celsius(fahrenheit):
return (fahrenheit - 32) * 5.0 / 9.0
# Main program
def main():
# Get user input
fahrenheit = float(input("Enter temperature in Fahrenheit: "))
# Convert to Celsius
celsius = fahrenheit_to_celsius(fahrenheit)
# Display the result
print(f"{fahrenheit}°F is equal to {celsius:.2f}°C")
# Run the program
if __name__ == "__main__":
main()
How to Run the Program
- Copy the Code: Copy the code above into a text editor or an Integrated Development Environment (IDE) like PyCharm or Visual Studio Code.
- Save the File: Save the file with a
.py
extension, for example, fahrenheit_to_celsius.py
.
- Run the Program:
- Open a command prompt or terminal.
- Navigate to the directory where you saved the file.
- Run the program by typing:
python fahrenheit_to_celsius.py
- Input Temperature: When prompted, enter a temperature in Fahrenheit, and the program will display the equivalent temperature in Celsius.
Example Output
Enter temperature in Fahrenheit: 100
100.0°F is equal to 37.78°C