In: Computer Science
Create a function that takes two numbers and a mathematical operator +, –, / , * and will perform a calculation with the given numbers.
Examples:
calculator(2, "+", 2) ➞ 4
calculator(2, "*", 2) ➞ 4
calculator(4, "/", 2) ➞ 2
Notes If the input tries to divide by 0, return: "Can't divide by 0!"
Code in C++ language ...
C++ CODE
#include <iostream>
#include <stdlib.h>
using namespace std;
void calculator(int num1, char symbol, int num2)//function used to solve mathematical operation
{
        switch(symbol)
        {
                case '+'://addition operatiob
                        cout << num1 + num2;
                        break;
                case '-'://subtraction operation
                        cout << num1 - num2;
                        break;
                case '*'://multiplication operation
                        cout << num1 * num2;
                        break;
                case '/'://division operation
                        if ( num2 == 0 )
                                cout << "Can't divide by 0!";
                        else
                                cout << num1 / num2;
                        break;
        }
}
int main()
{
        int num1, num2;
        char symbol;
        cout << "Enter mathematical operator:";
        cin >> symbol;//get operator from user
        cout << "Enter first number: ";
        cin >> num1;//get first number from user
        cout << "Enter second number: ";
        cin >> num2;//get second number from user
        
        calculator(num1, symbol, num2);
        return 0;
}
C++ CODE SCREENSHOT
OUTPUT
SCREENSHOT:
