C# program for Switch-Case



Now, This is the one of the best and ever like simple program of C# programming(Or may be any other programming language).

In this program i am giving an example of Addition, Subtraction, Multiplication, and Division to explain "Switch - Case" constructs.

ENJOY CODING!!!
_________________________________________________________________________________


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;



namespace Switch_Case

{

    class PostSlush

    {

        static void Main(string[] args)

        {



            int opt, no1, no2;

            float res;



        label:



            Console.WriteLine("\n\tMenu");

            Console.WriteLine("\nPress 1 for add");

            Console.WriteLine("Press 2 for subtraction");

            Console.WriteLine("Press 3 for multiplication");

            Console.WriteLine("Press 4 for Division");



            Console.Write("\n\nEnter first number:\t");

            no1 = Convert.ToInt32(Console.ReadLine());



            Console.Write("Enter second number:\t");

            no2 = Convert.ToInt32(Console.ReadLine());



            Console.Write("\nEnter your option:\t");

            opt = Convert.ToInt32(Console.ReadLine());



            switch (opt)

            {

                case 1:  //it means option number 1

                    res = no1 + no2;

                    Console.WriteLine("\n{0} + {1} = {2}", no1, no2, res);

                    break;



                case 2:  //it means option number 2

                    res = no1 - no2;

                    Console.WriteLine("\n{0} - {1} = {2}", no1, no2, res);

                    break;

                case 3:  //it means option number 3

                    res = no1 * no2;

                    Console.WriteLine("\n{0} * {1} = {2}", no1, no2, res);

                    break;

                case 4:  //it means option number 4

                    res = (float)no1 / no2;

                    Console.WriteLine("\n{0} / {1} = {2}", no1, no2, res);

                    break;

                default:  //(And, this is the default, it will execute when the input value from user did not meet with any given Case)

                    Console.WriteLine("\nInvalid option...Please try again.");

                    goto label; //give command to program go where the label is located in the program

            }

            Console.ReadLine();

        }

    }

}



Output :


Previous Post Next Post