Jump to content

Programming Fundamentals/Functions/C Sharp

From Wikiversity

functions.cs

[edit | edit source]
// This program asks the user for a Fahrenheit temperature, 
// converts the given temperature to Celsius,
// and displays the results.
//
// References:
//     https://www.mathsisfun.com/temperature-conversion.html
//     https://en.wikibooks.org/wiki/C_Sharp_Programming

using System;

class MainClass 
{
    public static void Main (string[] args) 
    {
        double fahrenheit;
        double celsius;
        
        fahrenheit = GetFahrenheit();
        celsius = CalculateCelsius(fahrenheit);
        DisplayResult(fahrenheit, celsius);
    }

    private static double GetFahrenheit()
    {
        string input;
        double fahrenheit;
        
        Console.WriteLine("Enter Fahrenheit temperature:");
        input = Console.ReadLine();
        fahrenheit = Convert.ToDouble(input);

        return fahrenheit;
    }

    private static double CalculateCelsius(double fahrenheit)
    {
        double celsius;
        
        celsius = (fahrenheit - 32) * 5 / 9;
        
        return celsius;
    }

    private static void DisplayResult(double fahrenheit, double celsius)
    {
        Console.WriteLine(fahrenheit.ToString() + "° Fahrenheit is " + celsius.ToString() + "° Celsius");
    }
}

Try It

[edit | edit source]

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

See Also

[edit | edit source]