In: Computer Science
C++
Write a program that has two functions.
The 1st function is the main function. The main function should prompt the user for three inputs: number 1, number 2, and an operator.
The main function should call a 2nd function called calculate. The 2nd function should offer the choices of calculating addition, subtraction, multiplication, and division. Use a switch statement to evaluate the operator, then choose the appropriate calculation and return the result to the main function.
CODE: C++ Programming Language
#include <iostream>
using namespace std;
int calculate(int, int, char); // Function Prototype
// Main function
int main(){
// Declaring required variables
int num1, num2;
char optr;
cout << "Enter number1: "; // Prompt the user
for number 1
cin >> num1;
cout << "Enter number2: "; // Prompt the user
for number 2
cin >> num2;
cout << "Enter operator: "; // Prompt the user
for Operator (+, -, *, /)
cin >> optr;
int result = calculate(num1, num2, optr); // Calling
the calculate function
cout << result;
return 0;
}
// Second function Calculate
int calculate(int num1, int num2, char optr){
// Switch statment to evaluate operator
switch(optr){
case '+':
return num1 +
num2;
break;
case '-':
return num1 -
num2;
break;
case '*':
return num1 *
num2;
break;
case '/':
return num1 /
num2;
break;
}
}
======================================================================
SCREENSHOT OF THE CODE:
======================================================================
OUTPUT:
Thank you. Please ask me if you have any doubt.