Junior Coding

Basic Calculator (Add, Subtract, Multiply, Divide) double Calculate(double a, double b, char op) { return op switch { '+' => a + b, Follow on: '-' => a - b, '*' => a * b, '/' => b != 0 ?

a / b : throw new DivideByZeroException(),

_ => throw new ArgumentException("Invalid operator"),

Explanation:

Simple switch statement performing basic arithmetic, with divide-by-zero check.

More from C# Programming Tutorial

All questions for this course