C# Program to Create Simple Calculator
switch to perform addition, subtraction, multiplication, or division, and prints the result.Examples
How to Think About It
switch. Finally, calculate and show the result, handling special cases like division by zero.Algorithm
Code
using System; class Calculator { static void Main() { Console.Write("Enter first number: "); double num1 = double.Parse(Console.ReadLine()); Console.Write("Enter second number: "); double num2 = double.Parse(Console.ReadLine()); Console.Write("Enter operator (+, -, *, /): "); char op = Console.ReadLine()[0]; double result = 0; bool valid = true; switch (op) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': if (num2 != 0) result = num1 / num2; else { Console.WriteLine("Cannot divide by zero."); valid = false; } break; default: Console.WriteLine("Invalid operator."); valid = false; break; } if (valid) Console.WriteLine($"Result: {result}"); } }
Dry Run
Let's trace the input 5, 3, + through the code
Read first number
num1 = 5
Read second number
num2 = 3
Read operator
op = '+'
Switch on operator
case '+': result = 5 + 3 = 8
Print result
Output: Result: 8
| Step | Variable | Value |
|---|---|---|
| 1 | num1 | 5 |
| 2 | num2 | 3 |
| 3 | op | + |
| 4 | result | 8 |
Why This Works
Step 1: Input reading
The program reads two numbers and an operator from the user using Console.ReadLine() and converts the numbers to double.
Step 2: Operation selection
It uses a switch statement on the operator to decide which arithmetic operation to perform.
Step 3: Division check
Before dividing, it checks if the second number is zero to avoid a runtime error and prints a message if division is not possible.
Step 4: Output result
If the operation is valid, it prints the result using string interpolation with $"Result: {result}".
Alternative Approaches
using System; class Calculator { static void Main() { Console.Write("Enter first number: "); double num1 = double.Parse(Console.ReadLine()); Console.Write("Enter second number: "); double num2 = double.Parse(Console.ReadLine()); Console.Write("Enter operator (+, -, *, /): "); char op = Console.ReadLine()[0]; double result = 0; bool valid = true; if (op == '+') result = num1 + num2; else if (op == '-') result = num1 - num2; else if (op == '*') result = num1 * num2; else if (op == '/') { if (num2 != 0) result = num1 / num2; else { Console.WriteLine("Cannot divide by zero."); valid = false; } } else { Console.WriteLine("Invalid operator."); valid = false; } if (valid) Console.WriteLine($"Result: {result}"); } }
using System; class Calculator { static double Calculate(double a, double b, char op) { return op switch { '+' => a + b, '-' => a - b, '*' => a * b, '/' => b != 0 ? a / b : throw new DivideByZeroException(), _ => throw new ArgumentException("Invalid operator") }; } static void Main() { Console.Write("Enter first number: "); double num1 = double.Parse(Console.ReadLine()); Console.Write("Enter second number: "); double num2 = double.Parse(Console.ReadLine()); Console.Write("Enter operator (+, -, *, /): "); char op = Console.ReadLine()[0]; try { double result = Calculate(num1, num2, op); Console.WriteLine($"Result: {result}"); } catch (Exception e) { Console.WriteLine(e.Message); } } }
Complexity: O(1) time, O(1) space
Time Complexity
The program performs a fixed number of operations regardless of input size, so it runs in constant time O(1).
Space Complexity
It uses a fixed amount of memory for variables and no extra data structures, so space complexity is O(1).
Which Approach is Fastest?
All approaches run in constant time; using a switch or if-else has negligible difference. Using a method with exceptions adds slight overhead but improves code clarity.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Switch statement | O(1) | O(1) | Simple and clear multiple operations |
| If-else statements | O(1) | O(1) | Simple logic, fewer cases |
| Method with switch expression | O(1) | O(1) | Clean code and error handling |