Python program to find Celsius to Fahrenheit and its reverse using if statement

Hello everyone, welcome to nkcoderz.com. In this article we will going to discuss about Python program to find Celsius to Fahrenheit and its reverse using if statement.

Program to find Celsius to Fahrenheit and its reverse using if statement

Here is a Python program that converts temperatures from Celsius to Fahrenheit and vice versa using an if statement:

Code for python program to find Celsius to Fahrenheit and its reverse using if statement

# Get the temperature and the conversion type from the user
temp = float(input("Enter the temperature: "))
conversion_type = input("Enter 'C' to convert from Celsius to Fahrenheit or 'F' to convert from Fahrenheit to Celsius: ")

# Convert the temperature
if conversion_type == 'C':
  converted_temp = (temp * 9/5) + 32
  print(f"{temp}°C is equal to {converted_temp}°F.")
elif conversion_type == 'F':
  converted_temp = (temp - 32) * 5/9
  print(f"{temp}°F is equal to {converted_temp}°C.")
else:
  print("Invalid conversion type.")

Output

Enter the temperature: 100

Enter 'C' to convert from Celsius to Fahrenheit or 'F' to convert from Fahrenheit to Celsius: C

100°C is equal to 212°F.

Explanation

This program first gets the temperature and the conversion type from the user. It then uses an if statement to check the conversion type and perform the appropriate conversion. If the conversion type is ‘C’, the program converts the temperature from Celsius to Fahrenheit.

If the conversion type is ‘F’, the program converts the temperature from Fahrenheit to Celsius. If the conversion type is neither ‘C’ nor ‘F’, the program prints an error message.

Python program to display marks cadre

Conclusion

If you liked this post, then please share this with your friends and make sure to bookmark this website for more awesome content.


If You Like This Page Then Make Sure To Follow Us on Facebook, G News and Subscribe Our YouTube Channel. We will provide you updates daily.
Shares
facebook sharing button Share
twitter sharing button Tweet
whatsapp sharing button Share
telegram sharing button Share
pinterest sharing button Pin

Leave a Comment