In: Computer Science
IN C++
Rather than clutter up the source code with blocks of essentially the same calculations or blocks of code controlled by IF then…else logic, we can move these blocks of code into one location that can be called from anywhere and anytime during the application’s execution, the “function”.
Functions can be passed “arguments”, one or more data values and return zero or one value to the calling routine Functions can call other separate functions or even themselves recursively.
If the divisor, n2, is zero, display a message, “Cannot divide by zero” in the function, default n2 to 1, return the dividend and display it in the main.
Example Output:
1 of 2 - Enter First Number: 12.34
2 of 2 - Enter Second Number: 8.4
1 - Add (12.34 + 8.4)
2 - Subtract (12.34 - 8.4)
3 - Multiplication (12.34 X 8.4)
4 - Divide (12.34 / 8.4)
5 – ALL
6 – Change Values
7 - Quit
Enter an Option (1 to 7): 3
12.34 X 8.4 = 103.656
Hit Any Key to continue...
1 - Add (2.4 + -3.4)
2 - Subtract (2.4 - -3.4)
3 - Multiplication (2.4 X -3.4)
4 - Divide (2.4 / -3.4)
5 - ALL
6 - Quit
Enter an Option (1 to 7): 5
2.4 + -3.4 = -1
2.4 - -3.4 = 5.8
-3.4 - 2.4 = -5.8
2.4 X -3.4 = -8.16
2.4 / -3.4 = -0.705882
Hit Any Key to continue...
Code
#include<iostream>
using namespace std;
void menu(double n1,double n2)
{
cout<<"\n1 - Add ( "<<n1<<" +
"<<n2<<" )"<<endl;
cout<<"2 - Subtract ( "<<n1<<" -
"<<n2<<" )"<<endl;
cout<<"3 - Multiplication ( "<<n1<<"
X "<<n2<<" )"<<endl;
cout<<"4 - Divide ( "<<n1<<" /
"<<n2<<" )"<<endl;
cout<<"5 - ALL"<<endl;
cout<<"6 - Change Values"<<endl;
cout<<"7 - Quit"<<endl;
cout<<"\tEnter an Option (1 to 7):";
}
double Add(double n1,double n2)
{
return n1+n2;
}
double Sub(double n1,double n2)
{
return n1-n2;
}
double Mul(double n1,double n2)
{
return n1*n2;
}
double Div(double n1,double n2)
{
if(n2==0)
{
cout<<"Cannot divide by
zero"<<endl;
n2=1;
}
return n1/n2;
}
int main()
{
double num1,num2;
cout<<"1 of 2 - Enter First Number: ";
cin>>num1;
cout<<"2 of 2 - Enter First Number: ";
cin>>num2;
int choice;
while (true)
{
menu(num1,num2);
cin>>choice;
cout<<endl;
if(choice==1)
{
cout<<num1<<" + "<<num2<<" =
"<<Add(num1,num2)<<endl;
}
else if(choice==2)
{
cout<<num1<<" - "<<num2<<" =
"<<Sub(num1,num2)<<endl;
}
else if(choice==3)
{
cout<<num1<<" * "<<num2<<" =
"<<Mul(num1,num2)<<endl;
}
else if(choice==4)
{
cout<<num1<<" / "<<num2<<" =
"<<Div(num1,num2)<<endl;
}
else if(choice==5)
{
cout<<num1<<" + "<<num2<<" =
"<<Add(num1,num2)<<endl;
cout<<num1<<" - "<<num2<<" =
"<<Sub(num1,num2)<<endl;
cout<<num1<<" * "<<num2<<" =
"<<Mul(num1,num2)<<endl;
cout<<num1<<" / "<<num2<<" =
"<<Div(num1,num2)<<endl;
}
else if(choice==6)
{
cout<<"1
of 2 - Enter First Number: ";
cin>>num1;
cout<<"2
of 2 - Enter First Number: ";
cin>>num2;
}
else if(choice==7)
break;
else
cout<<"Invalid choice\n";
}
return 1;
}
If you have any query regarding the code please ask me in the
comment i am here for help you. Please do not direct thumbs down
just ask if you have any query. And if you like my work then please
appreciates with up vote. Thank You.