0% found this document useful (0 votes)
3 views

Temperature Code

nothing

Uploaded by

kawairory27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Temperature Code

nothing

Uploaded by

kawairory27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

using System;

namespace Temperature
{
class TempChecker
{
public static void Main(string[] args)
{
while (true)
{
Console.WriteLine("Enter the temperature (or type 'exit' to quit):
"); string? inputTemperature = Console.ReadLine();

if (inputTemperature?.ToLower() == "exit")
{
break;
}

if (string.IsNullOrEmpty(inputTemperature))
{
Console.WriteLine("Invalid input. Please enter a valid
temperature."); continue;
}

if (!double.TryParse(inputTemperature, out double temperature))


{
Console.WriteLine("Invalid input. Please enter a valid numeric
temperature."); continue;
}

Console.WriteLine("Choose a conversion:");
Console.WriteLine("1. Celsius to Fahrenheit");
Console.WriteLine("2. Fahrenheit to Celsius");

string? inputChoice = Console.ReadLine();

if (string.IsNullOrEmpty(inputChoice) || !int.TryParse(inputChoice,
out int choice))
{
Console.WriteLine("Invalid choice. Please enter a valid
option."); continue;
}

double convertedTemperature;

if (choice == 1)
{
convertedTemperature = (temperature * 9 / 5) + 32;
Console.WriteLine($"{temperature} degrees Celsius is
{convertedTemperature} degrees Fahrenheit.");
}
else if (choice == 2)
{
convertedTemperature = (temperature - 32) * 5 / 9;
Console.WriteLine($"{temperature} degrees Fahrenheit is
{convertedTemperature} degrees Celsius.");
}
else
{
Console.WriteLine("Invalid Choice. Try again.");
}

Console.WriteLine();
}

Console.WriteLine("Goodbye!");
}
}
}

You might also like