# Very simple calculator with four basic operations using only if/else statements

Posted on

Problem

I created a simple calculator with multiplication, division, addition, and subtraction. Is there a way to write it shorter? I am a beginner so I do not know much about programming other than if…else statements and different data types.

``````        Console.WriteLine("Welcome to the simple calculator! The four operators of this calculator are +, -, *(multiply), and /(divide)");
Console.Write("Number: ");
Console.Write("Second Number: ");
Console.Write("Operator: ");

var numberConversion = Convert.ToInt64(number);
var secondNumberConversion = Convert.ToInt64(secondNumber);
var sum = numberConversion + secondNumberConversion; //Result of addition
var difference = numberConversion - secondNumberConversion; //Result of subtraction
var product = numberConversion * secondNumberConversion; //Result of multiplication
var quotient = numberConversion / secondNumberConversion; //Result of division

if (operatorType == "+")
{
Console.WriteLine(sum);
} else if (operatorType == "-")
{
Console.WriteLine(difference);
} else if (operatorType == "*")
{
Console.WriteLine(product);
} else if (operatorType == "/")
{
Console.WriteLine(quotient);
} else
{
}
``````

Solution

1. You need to check for input validity and have a mechanism for retry. Users can enter all sorts of things
2. Your variable names aren’t consistent. `number` and `secondNumber`. I would go with `firstNumber` and `secondNumber` in your scheme, but I would not use your scheme.
3. Your variable names are too verbose: `secondNumberConversion`. How about `n1` and `n2`?
4. A simple, elegant, maintainable and extensible solution is to use a Dictionary.

Not addressing 1. here is my version:

``````Console.WriteLine("Welcome to the simple calculator!"
+ " The four operators of this calculator are "
+ "+, -, *(multiply), and /(divide)");

Console.Write("First Number: ");

Console.Write("Second Number: ");

Console.Write("Operator: ");

var operatorsFuncMap = new Dictionary<string, Func<Int64, Int64, Int64>>
{
{"+", (a, b) => a + b},
{"-", (a, b) => a - b},
{"*", (a, b) => a * b},
{"/", (a, b) => a / b},
};

Console.WriteLine(operatorsFuncMap[operatorType](n1, n2));
``````

Here’s how I would do it:

``````        Console.WriteLine("Welcome to the simple calculator! The four operators of this calculator are +, -, *(multiply), and /(divide)");
Console.Write("Number: ");
Console.Write("Second Number: ");
Console.Write("Operator: ");

var numberConversion = Convert.ToInt64(number);
var secondNumberConversion = Convert.ToInt64(secondNumber);

switch (operatorType)
{
case "+":
Console.WriteLine(numberConversion + secondNumberConversion);
break;
case "-":
Console.WriteLine(numberConversion - secondNumberConversion);
break;
case "*":
Console.WriteLine(numberConversion * secondNumberConversion);
break;
case "/":
Console.WriteLine(numberConversion / secondNumberConversion);
break;
default:
More info here on `switch`: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/switch
I prefer using `switch` over a long `If Then` just because it’s easier to read.