Applied Programming/Functions/C Sharp

functions.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
///     https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/xmldoc/
/// </summary>

using System;

class MainClass
{
    /// <summary>Runs the main program logic.</summary>
    public static void Main (string[] args)
    {
        double fahrenheit;
        double celsius;

        fahrenheit = GetFahrenheit();
        celsius = ConvertFahrenheitToCelsius(fahrenheit);
        DisplayResult(fahrenheit, celsius);
    }

    /// <summary>Gets Fahrenheit temperature.</summary>
    /// <returns>Returns Fahrenheit temperature.</returns>
    private static double GetFahrenheit()
    {
        string input;
        double fahrenheit;

        Console.WriteLine("Enter Fahrenheit temperature: ");
        input = Console.ReadLine();
        fahrenheit = Convert.ToDouble(input);

        return fahrenheit;
    }

    /// <summary>Calculates Celsius temperature.</summary>
    /// <param name="fahrenheit">Fahrenheit temperature.</param>
    /// <returns>Returns Celsius temperature.</returns>
    private static double ConvertFahrenheitToCelsius(double fahrenheit)
    {
        double celsius;
        const int TemperatureDifference = 32;
        const double TemperatureRatio = 5.0 / 9.0;

        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