Here’s a complete Temperature Converter Console App in C#, along with a detailed explanation of how the code works.
This app allows users to convert between Celsius, Fahrenheit, and Kelvin.
Features
- Convert:
- Celsius to Fahrenheit/Kelvin
- Fahrenheit to Celsius/Kelvin
- Kelvin to Celsius/Fahrenheit
- Input validation
- User-friendly menu
- Repeats until the user chooses to exit
Full Code
using System; class TemperatureConverterApp { static void Main() { Console.WriteLine("Welcome to the Temperature Converter!"); bool exit = false; while (!exit) { Console.WriteLine("\nSelect an option:"); 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("Your choice: "); string input = Console.ReadLine(); if (!int.TryParse(input, out int choice) || choice < 1 || choice > 7) { Console.WriteLine("Invalid choice. Please enter a number from 1 to 7."); continue; } if (choice == 7) { exit = true; Console.WriteLine("Goodbye!"); break; } Console.Write("Enter the temperature value: "); string tempInput = Console.ReadLine(); if (!double.TryParse(tempInput, out double temperature)) { Console.WriteLine("Invalid temperature value. Please enter a numeric value."); continue; } double result = 0.0; string resultUnit = ""; switch (choice) { case 1: result = CelsiusToFahrenheit(temperature); resultUnit = "Fahrenheit"; break; case 2: result = CelsiusToKelvin(temperature); resultUnit = "Kelvin"; break; case 3: result = FahrenheitToCelsius(temperature); resultUnit = "Celsius"; break; case 4: result = FahrenheitToKelvin(temperature); resultUnit = "Kelvin"; break; case 5: result = KelvinToCelsius(temperature); resultUnit = "Celsius"; break; case 6: result = KelvinToFahrenheit(temperature); resultUnit = "Fahrenheit"; break; } Console.WriteLine($"Result: {result:F2} {resultUnit}"); } } // Conversion Methods static double CelsiusToFahrenheit(double c) => (c * 9 / 5) + 32; static double CelsiusToKelvin(double c) => c + 273.15; static double FahrenheitToCelsius(double f) => (f - 32) * 5 / 9; static double FahrenheitToKelvin(double f) => (f - 32) * 5 / 9 + 273.15; static double KelvinToCelsius(double k) => k - 273.15; static double KelvinToFahrenheit(double k) => (k - 273.15) * 9 / 5 + 32; }
Code Breakdown
using System;
Imports the System namespace to use Console and basic input/output features.
Main() Method
This is the entry point of the program where execution begins.
bool exit = false;
Initializes a loop control variable. The loop will keep running until the user chooses to exit.
User Menu
Displays a menu of options for the user to choose the type of conversion:
Console.WriteLine("1. Celsius to Fahrenheit"); ... Console.WriteLine("7. Exit");
The program reads and validates the user’s choice:
string input = Console.ReadLine(); int.TryParse(input, out int choice)
Reading Temperature Input
After selecting a conversion type, the user is prompted to enter a temperature value:
Console.Write("Enter the temperature value: ");
Input is parsed and validated using double.TryParse.
Switch Statement
Depending on the user’s selection, the correct method is called:
switch (choice) { case 1: result = CelsiusToFahrenheit(temperature); resultUnit = "Fahrenheit"; break; ... }
Conversion Methods
Defined at the bottom of the class, each method performs a standard conversion:
static double CelsiusToFahrenheit(double c) => (c * 9 / 5) + 32;
- All formulas follow well-known conversion equations:
- °F = (°C × 9/5) + 32
- °C = (°F − 32) × 5/9
- K = °C + 273.15
Displaying Results
After the conversion, the result is printed with two decimal places:
Console.WriteLine($"Result: {result:F2} {resultUnit}");
Example Output
Welcome to the Temperature Converter! Select an option: 1. Celsius to Fahrenheit ... 7. Exit Your choice: 1 Enter the temperature value: 100 Result: 212.00 Fahrenheit
How to Run This Program
- Open Visual Studio or another C# IDE.
- Create a new Console App project.
- Replace the contents of Program.cs with the code above.
- Run the application.