Thursday, August 12, 2010

Program to design calculator using switch case.


Write a program to design calculator using switch case.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Csharp
{
    /// <summary>
    /// Simple Switch Case Example
    /// </summary>
    class SimpleSwithch
    {
        public static void Main(string[] args)
        {
            Calculator obj = new Calculator();
            obj.cal();
            Console.ReadLine();
        }
    }
    class Calculator
    {
        public int a, b, sum,diff,mul,div;
        public char c;
        public void cal()
        {
        menu:
            Console.Clear();
            Console.WriteLine("=======================================");
            Console.WriteLine("Menu");
            Console.WriteLine("1.Addition");
            Console.WriteLine("2.Subtraction");
            Console.WriteLine("3.Multiplication");
            Console.WriteLine("4.Division");
            Console.WriteLine("=======================================");
         
            Console.WriteLine("Enter Your Option");
            c = Convert.ToChar(Console.ReadLine());
            Console.WriteLine("Enter the Values of a,b");
            a = Convert.ToInt32(Console.ReadLine());
            b = Convert.ToInt32(Console.ReadLine());
            switch (c)
            {
                case '1':
                    sum = a + b;
                    Console.WriteLine("{0}", sum);
                    break;
                case '2':
                    diff = a - b;
                    Console.WriteLine("{0}", diff);
                    break;
                case '3':
                    mul = a * b;
                    Console.WriteLine("{0}", mul);
                    break;
                case '4':
                    div = a / b;
                    Console.WriteLine("{0}", div);
                    break;
                   
            }
            Console.WriteLine("Enter M for Menu or Any other key to Exit");
            c = Convert.ToChar(Console.ReadLine());
            if (c == 'M')
            {
                goto menu;
            }
        }
    }
}

1 comment: