803
Here is a simple C# Temperature Converter program.
This program converts temperatures between Celsius, Fahrenheit, and Kelvin using a console-based interface.
Full Code
using System; class TemperatureConverter { static void Main(string[] args) { while (true) { Console.Clear(); Console.WriteLine("Temperature Converter"); Console.WriteLine("1. Celsius to Fahrenheit"); Console.WriteLine("2. Celsius to Kelvin"); Console.WriteLine("3. Fahrenheit to Celsius"); Console.WriteLine("4. Fahrenheit to Kelvin"); Console.WriteLine("5. Kelvin to Celsius"); Console.WriteLine("6. Kelvin to Fahrenheit"); Console.WriteLine("7. Exit"); Console.Write("Choose an option (1-7): "); string choice = Console.ReadLine(); if (choice == "7") { Console.WriteLine("Goodbye!"); break; } double inputTemperature = GetTemperatureInput(); double result = 0; switch (choice) { case "1": result = CelsiusToFahrenheit(inputTemperature); Console.WriteLine($"{inputTemperature}°C = {result:F2}°F"); break; case "2": result = CelsiusToKelvin(inputTemperature); Console.WriteLine($"{inputTemperature}°C = {result:F2}K"); break; case "3": result = FahrenheitToCelsius(inputTemperature); Console.WriteLine($"{inputTemperature}°F = {result:F2}°C"); break; case "4": result = FahrenheitToKelvin(inputTemperature); Console.WriteLine($"{inputTemperature}°F = {result:F2}K"); break; case "5": result = KelvinToCelsius(inputTemperature); Console.WriteLine($"{inputTemperature}K = {result:F2}°C"); break; case "6": result = KelvinToFahrenheit(inputTemperature); Console.WriteLine($"{inputTemperature}K = {result:F2}°F"); break; default: Console.WriteLine("Invalid choice! Please select a valid option."); break; } Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } } static double GetTemperatureInput() { double temperature; while (true) { Console.Write("Enter the temperature: "); string input = Console.ReadLine(); if (double.TryParse(input, out temperature)) { return temperature; } else { Console.WriteLine("Invalid input. Please enter a numeric value."); } } } // Conversion methods static double CelsiusToFahrenheit(double celsius) { return (celsius * 9 / 5) + 32; } static double CelsiusToKelvin(double celsius) { return celsius + 273.15; } static double FahrenheitToCelsius(double fahrenheit) { return (fahrenheit - 32) * 5 / 9; } static double FahrenheitToKelvin(double fahrenheit) { return (fahrenheit - 32) * 5 / 9 + 273.15; } static double KelvinToCelsius(double kelvin) { return kelvin - 273.15; } static double KelvinToFahrenheit(double kelvin) { return (kelvin - 273.15) * 9 / 5 + 32; } }
Explanation of the Code
- Program Structure:
- The program displays a menu to the user, allowing them to choose between various temperature conversions.
- It loops until the user selects the “Exit” option.
- Main Menu:
- Displays options to convert temperatures between Celsius, Fahrenheit, and Kelvin.
- Reads the user’s choice and processes it using a switch statement.
Console.WriteLine("1. Celsius to Fahrenheit"); Console.WriteLine("7. Exit"); string choice = Console.ReadLine();
- Temperature Input Validation:
- The method GetTemperatureInput() ensures the user provides a valid numeric value for the temperature. It uses double.TryParse to validate input.
static double GetTemperatureInput() { while (true) { Console.Write("Enter the temperature: "); if (double.TryParse(Console.ReadLine(), out double temperature)) { return temperature; } else { Console.WriteLine("Invalid input."); } } }
- Conversion Logic:
- Each conversion is implemented as a separate method for clarity and reusability.
- Formulas used:
- Celsius to Fahrenheit: F=C×95+32F = C \times \frac{9}{5} + 32
- Celsius to Kelvin: K=C+273.15K = C + 273.15
- Fahrenheit to Celsius: C=(F−32)×59C = (F – 32) \times \frac{5}{9}
- Fahrenheit to Kelvin: K=(F−32)×59+273.15K = (F – 32) \times \frac{5}{9} + 273.15
- Kelvin to Celsius: C=K−273.15C = K – 273.15
- Kelvin to Fahrenheit: F=(K−273.15)×95+32F = (K – 273.15) \times \frac{9}{5} + 32
- Switch Statement:
- Processes the user’s choice and calls the corresponding conversion method.
- Displays the result using formatted output ({result:F2} for two decimal places).
switch (choice) { case "1": result = CelsiusToFahrenheit(inputTemperature); Console.WriteLine($"{inputTemperature}°C = {result:F2}°F"); break; }
- Loop and Exit:
- The program runs in a loop (while (true)) until the user selects the “Exit” option.
if (choice == "7") { Console.WriteLine("Goodbye!"); break; }
Sample Output
Run the program:
Temperature Converter 1. Celsius to Fahrenheit 2. Celsius to Kelvin 3. Fahrenheit to Celsius 4. Fahrenheit to Kelvin 5. Kelvin to Celsius 6. Kelvin to Fahrenheit 7. Exit Choose an option (1-7): 1 Enter the temperature: 25 25°C = 77.00°F Press any key to continue...
Another run:
Choose an option (1-7): 4 Enter the temperature: 98.6 98.6°F = 310.15K Press any key to continue...
Exit:
Choose an option (1-7): 7 Goodbye!
Key Features
- Menu-Driven Interface:
- Easy-to-use text menu for selecting conversion options.
- Input Validation:
- Ensures the user enters a valid numeric value.
- Reusable Methods:
- Each conversion formula is implemented in a separate method, making the code modular and easy to maintain.
- Formatted Output:
- Displays results with two decimal places for better readability.
Ideas for Enhancement
- Add Support for Additional Units:
- Include Rankine or Réaumur scales for a comprehensive converter.
- GUI Version:
- Use Windows Forms or WPF to create a graphical interface for the temperature converter.
- Logging:
- Save all conversions to a file for future reference.
- Interactive CLI:
- Allow the user to perform multiple conversions without restarting the program.
This program provides a solid foundation for temperature conversion tasks and can be expanded for more advanced features!