Junior From PDF Coding C# Coding Interview

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
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details