कंप्यूटर प्रोग्रामिंग/कंडीशन/सी शार्प

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)
    {
        IfElse();
        SwitchCase();
    }

    private static void IfElse()
    {
        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 SwitchCase()
    {
        string choice;
        
        Console.WriteLine("Enter F to convert to Fahrenheit or C to convert to Celsius:");
        choice = Console.ReadLine();
        switch(choice)
        {
            case "C":
                ToCelsius();
                break;
            case "c":
                ToCelsius();
                break;
            case "F":
                ToFahrenheit();
                break;
            case "f":
                ToFahrenheit();
                break;
            default:
                Console.WriteLine("You must enter C to convert to Celsius or F to convert to Fahrenheit!");
                break;
        }
    }

    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");
    }
}

कोशिश करो सम्पादन

निम्न कोड मुफ्त ऑनलाइन विकास के वातावरण में से एक में ऊपर कॉपी और पेस्ट करो या अपने खुद के सी शार्प कम्पाइलर/इंटरप्रेटर/आईडीई का उपयोग करें।

यह भी देखें सम्पादन