Sunday, December 28, 2014

Program to show the use of switch case

8:43 PM - By azeheruddin khan 0

Description: - Switch case is a conditional construct which is used to select a statement or block of statements for execution from multiple statements or multiple blocks of statements based on the input value given by the user at runtime. Here, break statements are used in every case in order to jump out of the Switch case, if a particular case is satisfied.

Example:

using System;

namespace ArithmeticExample
{
    class Program
    {

        static void Main(string[] args)
        {
            char ch;
            Console.Write("specify the symbol of the operation to be performed:");
            ch = Convert.ToChar(Console.ReadLine());
            switch (ch)
            {
                case '+':
                    {
                        int a, b, c;
                        Console.Write("enter the value of a:");
                        a = Convert.ToInt32(Console.ReadLine());
                        Console.Write("enter the value of b:");
                        b = Convert.ToInt32(Console.ReadLine());
                        c = a + b;
                        Console.WriteLine("the sum is {0}", c);
                        break;
                    }
                case '-':
                    {
                        int a, b, c;
                        Console.Write("enter the value of a:");
                        a = Convert.ToInt32(Console.ReadLine());
                        Console.Write("enter the value of b:");
                        b = Convert.ToInt32(Console.ReadLine());
                        c = a - b;
                        Console.WriteLine("the difference is {0}", c);
                        break;
                    }
                case '*':
                    {
                        int a, b, c;
                        Console.Write("enter the value of a:");
                        a = Convert.ToInt32(Console.ReadLine());
                        Console.Write("enter the value of b:");
                        b = Convert.ToInt32(Console.ReadLine());
                        c = a * b;
                        Console.WriteLine("the product is {0}", c);
                        break;
                    }
                case '/':
                    {
                        int a, b, c;
                        Console.Write("enter the value of a:");
                        a = Convert.ToInt32(Console.ReadLine());
                        Console.Write("enter the value of b:");
                        b = Convert.ToInt32(Console.ReadLine());
                        c = a / b;
                        Console.WriteLine("the quotient is {0}", c);
                        break;
                    }

                case '%':
                    {
                        int a, b, c;
                        Console.Write("enter the value of a:");
                        a = Convert.ToInt32(Console.ReadLine());
                        Console.Write("enter the value of b:");
                        b = Convert.ToInt32(Console.ReadLine());
                        c = a % b;
                        Console.WriteLine("the remainder is {0}", c);
                        break;
                    }
                default:
                    {
                        Console.WriteLine("this is an invalid choice");
                        break;
                    }


            }
            Console.ReadLine();

        }
    }
}


Output:





About the Author

I am Azeheruddin Khan having more than 6 year experience in c#, Asp.net and ms sql.My work comprise of medium and enterprise level projects using asp.net and other Microsoft .net technologies. Please feel free to contact me for any queries via posting comments on my blog,i will try to reply as early as possible. Follow me @fresher2programmer
View all posts by admin →

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

adsense

© 2014 Fresher2Programmer. WP Theme-junkie converted by Bloggertheme9
Powered by Blogger.
back to top