Programming Course in C# ¡Free!

# Calculator, params and return value of Main

Proposed exercise

Create a program to calculate a sum, subtraction, product or division, analyzing the command line parameters:

calc 5 + 379

(Parameters must be a number, a sign, and another number; allowed signs are + - * x / )

This version must return the following error codes:
• 1 if the number of parameters is not 3
• 2 if the second parameter is not an accepted sign
• 3 if the first or third parameter is not a valid number
• 0 otherwise
Output

Solution

```using System;

public class Calc2
{
public static int Main()
{
if (args.Length != 3)
{
Console.WriteLine("Error!");
return 1;
}

try
{
int number1 = Convert.ToInt32(args[0]);
int number2 = Convert.ToInt32(args[2]);

switch (args[1])
{
case "+":
Console.WriteLine(number1 + number2);
break;
case "-":
Console.WriteLine(number1 - number2);
break;
case "/":
Console.WriteLine(number1 / number2);
break;
case "*":
case "x":
Console.WriteLine(number1 * number2);
break;
default:
Console.WriteLine("Error!");
return 2;

}
}
catch (Exception)
{
Console.WriteLine("Error!");
return 3;
}
return 0;
}
}
```