# Programming Fundamentals/Conditions/C Sharp

## conditions.cs

```// This program asks the user to select Fahrenheit or Celsius conversion
// and input a given temperature. Then the program converts the given
// temperature and displays the result.

// References:
//     https://www.mathsisfun.com/temperature-conversion.html
//     https://en.wikibooks.org/wiki/C_Sharp_Programming

using System;

public class MainClass
{
public static void Main(String[] args)
{
// main could either be an if-else structure or a switch-case structure

string choice;
double temperature;
double result;

Console.WriteLine("Enter F to convert to Fahrenheit or C to convert to Celsius:");

// if-else approach
if (choice == "C" || choice == "c")
{
temperature = GetTemperature("Fahrenheit");
result = CalculateCelsius(temperature);
DisplayResult(temperature, "Fahrenheit", result, "Celsius");
}
else if (choice == "F" || choice == "f")
{
temperature = GetTemperature("Celsius");
result = CalculateFahrenheit(temperature);
DisplayResult(temperature, "Celsius", result, "Fahrenheit");
}
else
{
Console.WriteLine("You must enter C to convert to Celsius or F to convert to Fahrenheit!");
}

// switch-case approach
switch(choice)
{
case "C":
case "c":
temperature = GetTemperature("Fahrenheit");
result = CalculateCelsius(temperature);
DisplayResult(temperature, "Fahrenheit", result, "Celsius");
break;
case "F":
case "f":
temperature = GetTemperature("Celsius");
result = CalculateFahrenheit(temperature);
DisplayResult(temperature, "Celsius", result, "Fahrenheit");
break;
default:
Console.WriteLine("You must enter C to convert to Celsius or F to convert to Fahrenheit!");
break;
}
}

private static double GetTemperature(string label)
{
string input;
double temperature;

Console.WriteLine("Enter " + label + " temperature:");
temperature = Convert.ToDouble(input);

return temperature;
}

private static double CalculateCelsius(double fahrenheit)
{
double celsius;

celsius = (fahrenheit - 32) * 5 / 9;

return celsius;
}

private static double CalculateFahrenheit(double celsius)
{
double fahrenheit;

fahrenheit = celsius * 9 / 5 + 32;

return fahrenheit;
}

private static void DisplayResult(double fahrenheit, string fromLabel, double celsius, string toLabel)
{
Console.WriteLine(fahrenheit.ToString() + "° " + fromLabel + " is " + celsius.ToString() + "° " + toLabel);
}
}
```

## Try It

Copy and paste the code above into one of the following free online development environments or use your own C Sharp compiler / interpreter / IDE.