﻿ C++ exercises and solutions: C++ functions

# C++ exercises and solutions: C++ functions

## C++ functions

### 1. Write a C++ program that will display the calculator menu.

The program will prompt the user to choose the operation choice (from 1 to 5). Then it asks the user to input two integer vales for the calculation. See the sample below.

2. Subtract
3. Multiply
4. Divide
5. Modulus
Enter your choice: 1
Enter your two numbers: 12 15
Result: 27

Continue? y

The program also asks the user to decide whether he/she wants to continue the operation. If he/she input ‘y’, the program will prompt the user to choose the operation gain. Otherwise, the program will terminate.

Solution:

#include <cstdlib>
#include <iostream>
#include<iomanip>

using namespace std;

cout<<"==================================================="<<"\n";
cout<<"==================================================="<<"\n";
cout<<"     2.Subtract"<<"\n";
cout<<"     3.Multiply"<<"\n";
cout<<"     4.Divide"<<"\n";
cout<<"     5.Modulus"<<"\n";
}
int Add(int a,int b){
return(a+b);
}

int Substract(int a, int b){
return(a-b);
}

int Multiply(int a, int b){
return(a*b);
}
float Divide(int a,int b){
return(a/b);
}
int Modulus(int a, int b){
return(a%b);
}

int main(int argc, char *argv[])
{
int yourchoice;
int a;
int b;
char confirm;
do
{
cin>>yourchoice;
cout<<"Enter your two integer numbers:";
cin>>a>>b;
cout<<"\n";
switch(yourchoice){
case 2:cout<<"Result:"<<Substract(a,b);break;
case 3:cout<<"Result:"<<Multiply(a,b);break;
case 4:cout<<"Result:"<<Divide(a,b);break;
case 5:cout<<"Result:"<<Modulus(a,b);break;
default:cout<<"invalid";
}

cout<<"\nPress y or Y to continue:";
cin>>confirm;
}while(confirm=='y'||confirm=='Y');
system("PAUSE");
return EXIT_SUCCESS;
}

### 2. Write a C++ program (using function overloaded) to sort 10 integer values, or 10 long values, or 10 double values.

Solution:

#include <cstdlib>
#include <iostream>
#include<iomanip>
#include<cmath>

using namespace std;

void mysort(int vallist[]){

int i,j;
int temp;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
if(vallist[j]>vallist[j+1]){
temp=vallist[j];
vallist[j]=vallist[j+1];
vallist[j+1]=temp;
}

for(i=0;i<10;i++)cout<<vallist[i]<<"\n";

}

void mysort(float vallist[]){

int i,j;
float temp;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
if(vallist[j]>vallist[j+1]){
temp=vallist[j];
vallist[j]=vallist[j+1];
vallist[j+1]=temp;
}

for(i=0;i<10;i++)cout<<vallist[i]<<"\n";

}
void mysort(double vallist[]){

int i,j;
double temp;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
if(vallist[j]>vallist[j+1]){
temp=vallist[j];
vallist[j]=vallist[j+1];
vallist[j+1]=temp;
}

for(i=0;i<10;i++)cout<<vallist[i]<<"\n";

}

int main(int argc, char *argv[])
{
int vallist[]={23,2,34,23,43,22,32,32,43,34};
mysort(vallist);
system("PAUSE");
return EXIT_SUCCESS; } Kim// Example program #include #include #include #include using namespace std; int main() { int x,y; char confirm; cout<<"MENU"; cout<<"\t 1. Add"<>option; cout<<"Ente your two numbers separated by space: "<>x>>y; //Add int a; a=x+y; //Sub int b; b=x-y; //Mul int c; c=x*y; //Div int d; d=x/y; //Mod int e; e=x%y; switch (option) { case 1: cout<>confirm; } while(confirm=='Y'); }2017-08-28 satish#include using namespace std; int add(int a,int k) { int c; c=a+k; cout<<"Your Ans Is : "<>a; cout<>b>>c; switch(a) { case 1: add(b,c); break; case 2: sub(b,c); break; case 3: mult(b,c); break; case 4: div(b,c); break; case 5: modual(b,c); break; default: cout<<"Invalid Option.."; } cout<>s; }while(s == 'Y'||s=='y'); }2017-06-09 AlberthQuestion 1 #include using namespace std; int ADD(int,int); int Result(int,int); int Product(int,int); int Quotient(int,int); int Mod(int); main () {int Num,Num1,Num2; int AddCALL,RelCALL,ProdCALL,QuoCALL,ModCALL,MenCALL; int Response; do{ cout<<"\n"; cout<<"****Advance Calculator****\n"; cout<<"\n"; cout<<"Enter 1. Add \n"; cout<<"Enter 2. Subtract \n"; cout<<"Enter 3. Multiply \n"; cout<<"Enter 4. Divide \n"; cout<<"Enter 5. Modulus \n"; cout<<"Please enter a Number from the Menu for the calculation that you want to perform: "; cin>>Num; cout<<"\n"; if (Num==1){ cout<<"Please enter 2 Numbers for your calculation"<<"\n"; cin>>Num1; cin>>Num2; AddCALL=ADD(Num1,Num2); cout<<"The Sum is "<>Num1; cin>>Num2; RelCALL=Result(Num1,Num2); cout<<"The Result is "<>Num1; cin>>Num2; ProdCALL=Product(Num1,Num2); cout<<"The Product is "<>Num1; cin>>Num2; QuoCALL=Quotient(Num1,Num2); cout<<"The Quocient is "<>Num; ModCALL=Mod(Num); cout<<"The Modulus of the number entered is "<>Response; }while (Response!=11); return 0; } int ADD(int N1,int N2) {int Sum; Sum=(N1+N2); return Sum; } int Result(int N1, int N2) {int RE; RE=N1-N2; return RE; } int Product(int N1, int N2) {int PR; PR=N1*N2; return PR; } int Quotient(int N1, int N2) {int QU; QU=N1/N2; return QU; } int Mod(int N) {int M; M=N%2; return M; }2017-04-27 AlberthQuestion 1 #include using namespace std; int ADD(int,int); int Result(int,int); int Product(int,int); int Quotient(int,int); int Mod(int); main () {int Num,Num1,Num2; int AddCALL,RelCALL,ProdCALL,QuoCALL,ModCALL,MenCALL; int Response; do{ cout<<"\n"; cout<<"****Advance Calculator****\n"; cout<<"\n"; cout<<"Enter 1. Add \n"; cout<<"Enter 2. Subtract \n"; cout<<"Enter 3. Multiply \n"; cout<<"Enter 4. Divide \n"; cout<<"Enter 5. Modulus \n"; cout<<"Please enter a Number from the Menu for the calculation that you want to perform: "; cin>>Num; cout<<"\n"; if (Num==1){ cout<<"Please enter 2 Numbers for your calculation"<<"\n"; cin>>Num1; cin>>Num2; AddCALL=ADD(Num1,Num2); cout<<"The Sum is "<>Num1; cin>>Num2; ProdCALL=Product(Num1,Num2); cout<<"The Product is "<>Num1; cin>>Num2; QuoCALL=Quotient(Num1,Num2); cout<<"The Quocient is "<>Num; ModCALL=Mod(Num); cout<<"The Modulus of the number entered is "<>Response; }while (Response!=11); return 0; } int ADD(int N1,int N2) {int Sum; Sum=(N1+N2); return Sum; } int Result(int N1, int N2) {int RE; RE=N1-N2; return RE; } int Product(int N1, int N2) {int PR; PR=N1*N2; return PR; } int Quotient(int N1, int N2) {int QU; QU=N1/N2; return QU; } int Mod(int N) {int M; M=N%2; return M; }2017-04-27 Ahmed_Afifyfor exercise 1, this is my code: #include using namespace std; enum operation {add = 1, subtract, multiply, divide}; void display() { cout << "Claculation MENU" << endl; cout << "---------" << endl; cout << "1. Add" << endl; cout << "2. Subtract" << endl; cout << "3. Multiply" << endl; cout << "4. Divide" << endl; cout << "5. Modulus" << endl; } double calculate(double &x, double &z) { int a; cout << "Enter your choice: " << endl; cin >> a; cout << "Enter your integers: " << endl; cin >> x >> z; if (a == add) return (x + z); else if (a == subtract) { return (x - z); } else if (a == multiply) { return (x*z); } else if (a == divide) { return (x / z); } else { cout << "you entered a non-valid option"; } } int main() { double a, b; char keep_calculating; display(); do { cout << calculate(a,b) << endl; cout << "Press (y or Y) to repeat / (n or N) to exit" << endl; cin >> keep_calculating; } while((keep_calculating == 'y')||(keep_calculating == 'Y')); system("pause"); return 0; } 2017-03-14 Ferran PérezFor exercise 1, my solution was the following: #include #include #include #include using namespace std; // Addition int add(int a, int b){ return a+b; } // Subtraction int subtract(int a, int b){ return a-b; } // Multiplication int multiply(int a, int b){ return (a*b); } // Division int divide(int a, int b){ return (a/b); } // Modulus int modulusNum(int a, int b){ return (a%b); } int main() { int option; char keepComputing = 'y'; int a, b; cout << "Welcome to the calculator!" << endl; // Display options do{ cout << "MENU" << endl; cout << "1. Addition (a+b)"<< endl; cout << "2. Subtraction (a-b)"<< endl; cout << "3. Multiplication (a*b)"<< endl; cout << "4. Division (a/b)"<< endl; cout << "5. Modulus (a%b)"<< endl; cout << endl; cout << "Please, select an option by typing its corresponding number" << endl; // Read selected option and compute, then ask if the user wants to continue or exit cin >> option; cout << "Please, introduce the numbers" << endl; cin >> a >> b; switch (option){ case 1: cout << "Addition result: " << add(a,b) << endl; break; case 2: cout << "Result: " << subtract(a,b) << endl; break; case 3: cout << "Result: " << multiply(a,b) << endl; break; case 4: cout << "Result: " << divide(a,b) << endl; break; case 5: cout << "Result: " << modulusNum(a,b) << endl; break; default: cout << "Invalid option, please select one of the provided list (1-5)" << endl; } // Prompt user to continue or exit cout << "Do you want to continue (y) or exit (n)?"; cin >> keepComputing; }while(keepComputing == 'y' || keepComputing == 'Y'); cout << "Program exited successfully" << endl; return 0; } Hope it can help anyone looking for a different solution that the one proposed by the author. Cheers.2017-03-06 imran israr/* The program will prompt the user to choose the operation choice (from 1 to 5). Then it asks the user to input two integer vales for the calculation. See the sample below. MENU 1. Add 2. Subtract 3. Multiply 4. Divide 5. Modulus Enter your choice: 1 Enter your two numbers: 12 15 Result: 27 */ /* using with function*/ #include using namespace std; int add(int,int); int sub(int, int); int div(int ,int); int mul(int ,int); int main() { int input,a,b; cout<<"please enter the number within the range (1 to 4):"<<"\t"; cin>>input; if(input<=4) { cout<<"\n"; cout<<"please enter the value of a:"<<"\t"; cin>>a; cout<<"please enter the value of b:"<<"\t"; cin>>b; cout<<"--------------------------------------"<<"\n"; if(input==1) { cout<<"add function is call:"<<"\t"<<"\n"; add(a,b); cout<<"--------------------------------------"<<"\n"; } else if(input==2) { cout<<"subtract function is call:"<<"\t"<<"\n"; sub(a,b); cout<<"--------------------------------------"<<"\n"; } else if(input==3) { cout<<"Multiplication function is call:"<<"\t"<<"\n"; mul(a,b); cout<<"--------------------------------------"<<"\n"; } else if(input==4) { cout<<"Division function is call:"<<"\t"<<"\n"; div(a,b); cout<<"--------------------------------------"<<"\n"; } } else { cout<<"Not within the range:"<<"\t"<<"\n"; cout<<"--------------------------------------"<<"\n"; } system("pause"); } int add(int a,int b) { int result; result=a+b; cout<<"\t"<<"Add:"<<"\t"< #include using namespace std; int add(); int sub(); int mul(); int div(); int mod(); int main() { int num,add1,add2,add3,add4,add5; do { cout<<"\n\n\n menu"; cout<<"\n1:add"; cout<<"\n2:sub"; cout<<"\n3:mul"; cout<<"\n4:div"; cout<<"\n5:modulus"; cout<<"\n0:to exit"; cout<<"\nenter your choice\n"; cin>>num; switch(num) { case 1: add1=add(); cout<<"the sum of two number is "<>a; cout<<"enter second value\n"; cin>>b; return a+b; } int sub () { int a,b; cout<<"enter first value\n"; cin>>a; cout<<"enter second value\n"; cin>>b; return a-b; } int mul () { int a,b; cout<<"enter first value\n"; cin>>a; cout<<"enter second value\n"; cin>>b; return a*b; } int div () { int a,b; cout<<"enter first value\n"; cin>>a; cout<<"enter second value\n"; cin>>b; return a/b; } int mod () { int a,b; cout<<"enter first value\n"; cin>>a; cout<<"enter second value\n"; cin>>b; return a%b; } 2016-06-14 mohd shariqHey guys ! Amazing website, helps too mu ch ,increase skills.2016-04-11 Usman Shahzadthankssss alo0ot..!! this website helps me alo0t... and also increase my skill..!! 2016-01-16 Junaidvery helpful for beginers like me2015-12-26 pariwow this website is really great. 2015-10-02 fccoding is so fucking hard2015-09-28 maic this website helps me to get more!! 2015-05-29 claude ndizeyehow to use for loop,while,function,nested loop.2015-05-12 AhmadThanks ...its help me alot,,,2015-02-28 Charles Giraits better to use this website!!!2014-03-25