Python program to convert Celsius to Fahrenheit and vice versa

One of the most common calculations in science and engineering is the conversion of temperature between Celsius and Fahrenheit. In this article, we will discuss different ways to write a Python program to convert temperatures between Celsius and Fahrenheit using functions, classes, and loops.

Python program to convert celsius to fahrenheit and fahrenheit to celsius

def celsius_to_fahrenheit(celsius):
    return (celsius * 9/5) + 32

def fahrenheit_to_celsius(fahrenheit):
    return (fahrenheit - 32) * 5/9

celsius = 25
fahrenheit = celsius_to_fahrenheit(celsius)
print("Temperature in Fahrenheit: ", fahrenheit)

fahrenheit = 70
celsius = fahrenheit_to_celsius(fahrenheit)
print("Temperature in Celsius: ", celsius)

python program to convert celsius to fahrenheit using functions

def celsius_to_fahrenheit(celsius):
    return (celsius * 9/5) + 32

celsius = 25
fahrenheit = celsius_to_fahrenheit(celsius)
print("Temperature in Fahrenheit: ", fahrenheit)

python program to convert celsius to fahrenheit and vice versa

def celsius_to_fahrenheit(celsius):
    return (celsius * 9/5) + 32

def fahrenheit_to_celsius(fahrenheit):
    return (fahrenheit - 32) * 5/9

celsius = 25
fahrenheit = celsius_to_fahrenheit(celsius)
print("Temperature in Fahrenheit: ", fahrenheit)

fahrenheit = 70
celsius = fahrenheit_to_celsius(fahrenheit)
print("Temperature in Celsius: ", celsius)

python program to convert celsius to fahrenheit using class

class TemperatureConverter:
    def celsius_to_fahrenheit(self, celsius):
        return (celsius * 9/5) + 32
    
    def fahrenheit_to_celsius(self, fahrenheit):
        return (fahrenheit - 32) * 5/9

converter = TemperatureConverter()
celsius = 25
fahrenheit = converter.celsius_to_fahrenheit(celsius)
print("Temperature in Fahrenheit: ", fahrenheit)

fahrenheit = 70
celsius = converter.fahrenheit_to_celsius(fahrenheit)
print("Temperature in Celsius: ", celsius)

write a program that converts celsius temperatures to fahrenheit temperatures

def celsius_to_fahrenheit(celsius):
    return (celsius * 9/5) + 32

celsius = 25
fahrenheit = celsius_to_fahrenheit(celsius)
print("Temperature in Fahrenheit: ", fahrenheit)

python program to convert celsius to fahrenheit with exception handling

This Python program converts a temperature from Celsius to Fahrenheit. The user is prompted to enter a temperature in Celsius, which is then passed as an argument to a function called celsius_to_fahrenheit. The function performs the conversion and returns the result in Fahrenheit. The program also includes exception handling, which is used to catch any errors that may occur during the execution of the program. Specifically, the function raises a ValueError exception if the input temperature is below absolute zero. The try and except blocks are used to catch this exception and display an error message to the user.

# python program to convert celsius to fahrenheit with exception handling

def celsius_to_fahrenheit(celsius):
    #convert celsius to fahrenheit
    if celsius < -273.15:
        raise ValueError("Temperature below absolute zero")
    else:
        fahrenheit = (celsius * 9/5) + 32
        return fahrenheit

try:
    celsius = float(input("Enter temperature in Celsius: "))
    fahrenheit = celsius_to_fahrenheit(celsius)
    print("Temperature in Fahrenheit: {:.2f}".format(fahrenheit))
except ValueError as e:
    print(e)

Leave a Comment