Q:

C# | Design a simple calculator using switch case statement

0

C# | Design a simple calculator using switch case statement

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

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

namespace DecisionMaking3
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Calculator");
            Console.WriteLine("----------------------------");
            Console.WriteLine("1.Add");
            Console.WriteLine("2.Substract");
            Console.WriteLine("3.Multiply");
            Console.WriteLine("4.Divide");
            Console.Write("Enter Choice(1-4):");
            int ch = Int32.Parse(Console.ReadLine());
            int a, b, c;
            switch(ch)
            {
                case 1:
                    Console.Write("Enter A:");
                    a = Convert.ToInt32(Console.ReadLine());
                    Console.Write("Enter B:");
                    b = Convert.ToInt32(Console.ReadLine());
                    c = a + b;
                    Console.WriteLine("Sum = {0}", c);
                    break;
                case 2:
                    Console.Write("Enter A:");
                    a = Convert.ToInt32(Console.ReadLine());
                    Console.Write("Enter B:");
                    b = Convert.ToInt32(Console.ReadLine());
                    c = a - b;
                    Console.WriteLine("Difference = {0}", c);
                    break;
                case 3:
                    Console.Write("Enter A:");
                    a = Convert.ToInt32(Console.ReadLine());
                    Console.Write("Enter B:");
                    b = Convert.ToInt32(Console.ReadLine());
                    c = a * b;
                    Console.WriteLine("Product = {0}", c);
                    break;
                case 4:
                    Console.Write("Enter A:");
                    a = Convert.ToInt32(Console.ReadLine());
                    Console.Write("Enter B:");
                    b = Convert.ToInt32(Console.ReadLine());
                    c = a / b;
                    Console.WriteLine("Quotient = {0}", c);
                    break;
                default:
                    Console.WriteLine("Invalid Choice");
                    break;
            }
            Console.ReadKey();
        }
    }
}

Output

First run:

Calculator
----------------------------
1.Add
2.Substract
3.Multiply
4.Divide
Enter Choice(1-4):1
Enter A:10
Enter B:20
Sum = 30


Second run:

Calculator
----------------------------
1.Add
2.Substract
3.Multiply
4.Divide
Enter Choice(1-4):4
Enter A:10
Enter B:20
Quotient = 0


Third run:

Calculator
----------------------------
1.Add
2.Substract
3.Multiply
4.Divide
Enter Choice(1-4):5
Invalid Choice

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Using string with switch case statement in C#... >>
<< C# | Design a simple calculator using if else if s...