Function calculator, params of Main Learn programming C# Sharp

Lesson:

Functions


Exercise:

Function calculator, params of Main 11


Objetive:

Create a C# 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 / )


Code:

using System;
public class exercise125
{
    static void Main()
    {
        char operation = Convert.ToChar(args[1]);

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

        int result = 0;
        if (args.Length != 3)
        {
            switch (operation)
            {
                case '+':
                    result = number1 + number2;
                    break;
                case '-':
                    result = number1 - number2;
                    break;
                case 'x':
                case 'X':
                case '*':
                    result = number1 * number2;
                    break;
                case '/':
                    result = number1 / number2;
                    break;
            }
            Console.WriteLine("Result: {0}", result);
        }
        else
        Console.WriteLine("Error in arguments");
    }
}