Project Simple Calculator
Create a simple calculator that supports addition, subtraction, multiplication, division, and modulus.
It uses decimal for precise decimal arithmetic (avoiding floating‑point errors like 0.1 + 0.2).
Console.WriteLine("Simple Calculator");Console.WriteLine("Operations: +, -, *, /, %");Console.Write("Enter first number: ");decimal a = decimal.Parse(Console.ReadLine());Console.Write("Enter operator (+, -, *, /, %): ");string op = Console.ReadLine();Console.Write("Enter second number: ");decimal b = decimal.Parse(Console.ReadLine());
decimal result = 0;bool valid = true;
switch (op){ case "+": result = a + b; break; case "-": result = a - b; break; case "*": result = a * b; break; case "/": if (b != 0) result = a / b; else { Console.WriteLine("Error: Division by zero"); valid = false; } break; case "%": result = a % b; break; default: Console.WriteLine("Invalid operator"); valid = false; break;}
if (valid) Console.WriteLine($"Result: {a} {op} {b} = {result}");Run the Application
Section titled “Run the Application”dotnet runResult (example: 10 + 5)
Section titled “Result (example: 10 + 5)”Simple CalculatorOperations: +, -, *, /, %Enter first number: 10Enter operator (+, -, *, /, %): +Enter second number: 5Result: 10 + 5 = 15Result (example: 0.1 + 0.2 – no floating‑point error)
Section titled “Result (example: 0.1 + 0.2 – no floating‑point error)”Simple CalculatorOperations: +, -, *, /, %Enter first number: 0.1Enter operator (+, -, *, /, %): +Enter second number: 0.2Result: 0.1 + 0.2 = 0.3Result (example: division by zero)
Section titled “Result (example: division by zero)”Simple CalculatorOperations: +, -, *, /, %Enter first number: 7Enter operator (+, -, *, /, %): /Enter second number: 0Error: Division by zero