Please develop a program using C ++ following the guidelines below: 1. The progr
ID: 3756788 • Letter: P
Question
Please develop a program using C ++ following the guidelines below:
1. The program should have a functions to add two int variables, and a functions to add two double variables.
2. The program should have a functions to subtract two int variables, and a functions to subtract two double variables.
3. The program should have a functions to multiply two int variables, and a functions to multiply two double variables.
4. The program should have a functions to divide two int variables, and a functions to divide two double variables.
5. A function to provide factorial value of a given number
Conditions:
The program should be menu based and users should be able to select a mathematical operation (e.g., +, -, *, /, !). If a user enters "!", then your program should ask for a single variable (otherwise, two variables). If a user enters a random character, then ask the user to enter a correct character.
Explanation / Answer
CODE
#include <iostream>
using namespace std;
int add(int a, int b) {
return a + b;
}
int sub(int a, int b) {
return a - b;
}
int mul(int a, int b) {
return a * b;
}
int divide(int a, int b) {
return a / b;
}
double add(double a, double b) {
return a + b;
}
double sub(double a, double b) {
return a - b;
}
double mul(double a, double b) {
return a * b;
}
double divide(double a, double b) {
return a / b;
}
int factorial(int a) {
if(a == 1) return 1;
return a * factorial(a - 1);
}
int main() {
while(true) {
cout << "Choose 1 to add 2 integers" << endl;
cout << "Choose 2 to sub 2 integers" << endl;
cout << "Choose 3 to mul 2 integers" << endl;
cout << "Choose 4 to divide 2 integers" << endl;
cout << "Choose 5 to add 2 float values" << endl;
cout << "Choose 6 to sub 2 float values" << endl;
cout << "Choose 7 to mul 2 float values" << endl;
cout << "Choose 8 to divide 2 float values" << endl;
cout << "Choose 9 to find factorial" << endl;
int flag = 0;
int choice;
cin >> choice;
int a,b;
double c,d;
switch(choice) {
case 1:
cin >> a >> b;
printf("%d ", add(a , b));
break;
case 2:
cin >> a >> b;
printf("%d ", sub(a , b));
break;
case 3:
cin >> a >> b;
printf("%d ", mul(a , b));
break;
case 4:
cin >> a >> b;
printf("%d ", divide(a , b));
break;
case 5:
cin >> c >> d;
printf("%f ", add(c,d));
break;
case 6:
cin >> c >> d;
printf("%f ", sub(c,d));
break;
case 7:
cin >> c >> d;
printf("%f ", mul(c,d));
break;
case 8:
cin >> c >> d;
printf("%f ", divide(c,d));
break;
case 9:
int e;
cin >> e;
printf("%d ", factorial(e));
break;
default:
flag = 1;
}
if(flag == 0) {
break;
}
}
}