Applied Programming/Loops/C Sharp

loops.cs

edit
/// <summary>
/// This program asks the user for a Fahrenheit temperature, 
/// converts the given temperature to Celsius,
/// and displays the results.
///
/// Input:
///     Fahrenheit temperature
///
/// Output:
///     Fahrenheit temperature
///    Celsius temperature
///
/// Example:
///    Enter Fahrenheit temperature:
///    100
///    100.0° Fahrenheit is 37.8° Celsius
///
/// References:
///     https://www.mathsisfun.com/temperature-conversion.html
///     https://en.wikibooks.org/wiki/C_Sharp_Programming
///     https://en.wikiversity.org/wiki/Applied_Programming/Functions/Python3
///     https://en.wikiversity.org/wiki/Programming_Fundamentals/Functions/C_Sharp
///     https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/coding-conventions
///     https://www.dofactory.com/reference/csharp-coding-standards
/// </summary>

using System;

class MainClass
{
    const int TemperatureDifference = 32;
    const double TemperatureRatio = 5.0 / 9.0;
    const double AbsoluteZero = -459.67;

    /// <summary>Runs the main program logic.</summary>
    public static void Main(string[] args)
    {
        while(true)
        {
            Console.Clear();
            var fahrenheit = GetFahrenheit();
            var celsius = ConvertFahrenheitToCelsius(fahrenheit);
            DisplayResult(fahrenheit, celsius);

            Console.WriteLine("Would you like to convert another temperature? Y or N");
            var input = Console.ReadLine();
            if (input != "Y") 
			{ 
				break;
			}
        }
    }

    /// <summary>Gets Fahrenheit temperature.</summary>
    /// <returns>Returns Fahrenheit temperature.</returns>
    /// <exception cref="System.ArgumentOutOfRangeException">
    ///    Thrown when entered temperature is below absolute zero.
    /// </exception>
    /// <exception cref="System.FormatException">
    ///    Thrown when entered temperature is not a valid floating point value.
    /// </exception>
    private static double GetFahrenheit()
    {
        while(true)
        {
            Console.WriteLine("Enter Fahrenheit temperature: ");
            var input = Console.ReadLine();

            try
            {
                var fahrenheit = Convert.ToDouble(input);
                if (fahrenheit < AbsoluteZero)
                {
                    Console.WriteLine("Fahrenheit temperature cannot be below absolute zero.");
                    Console.WriteLine($"ArgumentOutOfRangeException: '{fahrenheit}' is invalid.");
                }
                else
                {
                    return fahrenheit;
                }
            }
            catch (System.FormatException)
            {
                Console.WriteLine("Fahrenheit temperature must be a floating point value.");
                Console.WriteLine($"FormatException: '{input}' is invalid.");
            }
        }
    }

    /// <summary>Calculates Celsius temperature.</summary>
    /// <param name="fahrenheit">Fahrenheit temperature.</param>
    /// <returns>Returns Celsius temperature.</returns>
    private static double ConvertFahrenheitToCelsius(double fahrenheit)
    {
        System.Diagnostics.Debug.Assert(fahrenheit >= AbsoluteZero);

        var celsius = (fahrenheit - TemperatureDifference) * TemperatureRatio;

        return celsius;
    }

    /// <summary>Displays conversion results.</summary>
    /// <param name="fahrenheit">Fahrenheit temperature.</param>
    /// <param name="fahrenheit">Celsius temperature.</param>
    private static void DisplayResult(double fahrenheit, double celsius)
    {
        Console.WriteLine(fahrenheit.ToString() + "° Fahrenheit is " + celsius + "° Celsius");
    }
}

Try It

edit

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

See Also

edit