Programming Fundamentals/Conditions/C Sharp: Difference between revisions

From Wikiversity
Jump to navigation Jump to search
Content deleted Content added
Creating
(No difference)

Revision as of 00:05, 2 February 2017

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.

using System;

public class MainClass
{
    public static void Main(String[] args)
    {
        string choice;
        
        Console.WriteLine("Enter F to convert to Fahrenheit or C to convert to Celsius:");
        choice = Console.ReadLine();
        if (choice == "C" || choice == "c")
        {
            ToCelsius();
        }
        else if (choice == "F" || choice == "f")
        {
            ToFahrenheit();
        }
        else
        {
            Console.WriteLine("You must enter C to convert to Celsius or F to convert to Fahrenheit!");
        }
    }

    private static void ToCelsius()
    {
        double f;
        double c;
        
        Console.WriteLine("Enter Fahrenheit temperature:");
        f = Convert.ToDouble(Console.ReadLine());
        c = (f - 32) * 5 / 9;
        Console.WriteLine(f.ToString() + "° Fahrenheit is " + c + "° Celsius");
    }

    private static void ToFahrenheit()
    {
        double c;
        double f;
        
        Console.WriteLine("Enter Celsius temperature:");
        c = Convert.ToDouble(Console.ReadLine());
        f = c * 9 / 5 + 32;
        Console.WriteLine(c.ToString() + "° Celsius is " + f + "° Fahrenheit");
    }
}

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.

See Also