|
| 1 | +# Temperature Converter |
| 2 | +# Converts temperature between Celsius, Fahrenheit, and Kelvin |
| 3 | + |
| 4 | +def celsius_to_fahrenheit(celsius): |
| 5 | + return (celsius * 9 / 5) + 32 |
| 6 | + |
| 7 | + |
| 8 | +def fahrenheit_to_celsius(fahrenheit): |
| 9 | + return (fahrenheit - 32) * 5 / 9 |
| 10 | + |
| 11 | + |
| 12 | +def celsius_to_kelvin(celsius): |
| 13 | + return celsius + 273.15 |
| 14 | + |
| 15 | + |
| 16 | +def kelvin_to_celsius(kelvin): |
| 17 | + return kelvin - 273.15 |
| 18 | + |
| 19 | + |
| 20 | +def fahrenheit_to_kelvin(fahrenheit): |
| 21 | + return (fahrenheit - 32) * 5 / 9 + 273.15 |
| 22 | + |
| 23 | + |
| 24 | +def kelvin_to_fahrenheit(kelvin): |
| 25 | + return (kelvin - 273.15) * 9 / 5 + 32 |
| 26 | + |
| 27 | + |
| 28 | +def main(): |
| 29 | + print("🌡️ Temperature Converter") |
| 30 | + print("1. Celsius to Fahrenheit") |
| 31 | + print("2. Fahrenheit to Celsius") |
| 32 | + print("3. Celsius to Kelvin") |
| 33 | + print("4. Kelvin to Celsius") |
| 34 | + print("5. Fahrenheit to Kelvin") |
| 35 | + print("6. Kelvin to Fahrenheit") |
| 36 | + |
| 37 | + choice = input("Enter choice (1–6): ") |
| 38 | + |
| 39 | + if choice == "1": |
| 40 | + c = float(input("Enter Celsius: ")) |
| 41 | + print(f"{c}°C = {celsius_to_fahrenheit(c):.2f}°F") |
| 42 | + elif choice == "2": |
| 43 | + f = float(input("Enter Fahrenheit: ")) |
| 44 | + print(f"{f}°F = {fahrenheit_to_celsius(f):.2f}°C") |
| 45 | + elif choice == "3": |
| 46 | + c = float(input("Enter Celsius: ")) |
| 47 | + print(f"{c}°C = {celsius_to_kelvin(c):.2f}K") |
| 48 | + elif choice == "4": |
| 49 | + k = float(input("Enter Kelvin: ")) |
| 50 | + print(f"{k}K = {kelvin_to_celsius(k):.2f}°C") |
| 51 | + elif choice == "5": |
| 52 | + f = float(input("Enter Fahrenheit: ")) |
| 53 | + print(f"{f}°F = {fahrenheit_to_kelvin(f):.2f}K") |
| 54 | + elif choice == "6": |
| 55 | + k = float(input("Enter Kelvin: ")) |
| 56 | + print(f"{k}K = {kelvin_to_fahrenheit(k):.2f}°F") |
| 57 | + else: |
| 58 | + print("Invalid choice ❌") |
| 59 | + |
| 60 | + |
| 61 | +if __name__ == "__main__": |
| 62 | + main() |
0 commit comments