In this program, you’ll learn to make a simple calculator using switch..case in C# Console Application.
This calculator would be able to add, subtract, multiply and divide two numbers.
Final Preview
Source 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 40 41 42 43 44 45 46 47 48 |
static void Main(string[] args) { double num1; double num2; string operand; ConsoleKeyInfo status; double answer; while (true) { Console.Write("Enter First Number : "); num1 = Convert.ToDouble(Console.ReadLine()); Console.Write("Enter Second Number : "); num2 = Convert.ToDouble(Console.ReadLine()); Console.Write("Please enter an operand (+, -, /, *): "); operand = Console.ReadLine(); switch (operand) { case "-": answer = num1 - num2; break; case "+": answer = num1 + num2; break; case "/": answer = num1 / num2; break; case "*": answer = num1 * num2; break; default: answer = 0; break; } Console.WriteLine(num1.ToString() + " " + operand + " " + num2.ToString() + " = " + answer.ToString()); Console.WriteLine("\n\n Do You Want To Break (Y/y)"); status = Console.ReadKey(); if (status.Key == ConsoleKey.Y) { break; } Console.Clear(); } } |