In this program we will learn how to make a simple calculator using switch case that performs addition, subtraction, multiplication and division based on the user input.
Output:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | class Program { static void Main(string[] args) { double num1, num2,result=0; Console.Write("Enter first number:"); num1 = Convert.ToDouble(Console.ReadLine()); Console.Write("Enter second number:"); num2 = Convert.ToDouble(Console.ReadLine()); Console.Write("Enter an operator (+, -, *, /): "); char operand = Console.ReadKey().KeyChar; Console.WriteLine(); switch (operand) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': result = num1 / num2; break; default:Console.WriteLine("You have entered wrong operator"); break; } Console.WriteLine(num1 + " " +operand+" " + num2 + ": " + result); Console.ReadLine(); } } |