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.
 

MENU
          1. Add
          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;
 
void displaymenu(){
cout<<"==================================================="<<"\n";
cout<<"                         MENU                                  "<<"\n";
cout<<"==================================================="<<"\n";
cout<<"     1.Add"<<"\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[])
{
 //show menu
displaymenu();
int yourchoice;
int a;
int b;
char confirm;
do
{
cout<<"Enter your choice(1-5):";
cin>>yourchoice;
cout<<"Enter your two integer numbers:";
cin>>a>>b;
cout<<"\n";
switch(yourchoice){
 case 1:cout<<"Result:"<<Add(a,b);break;
 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; }


Comments

Kim comment

 Kim

// Example program
#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>
using namespace std;

int main()
{
int x,y;
char confirm;
cout<<"MENU";
cout<<"\t 1. Add"<<endl;
cout<<"\t 2. Subtract"<<endl;
cout<<"\t 3. Multiply"<<endl;
cout<<"\t 4. Divide"<<endl;
cout<<"\t 5. Modulus "<<endl;
do{
cout<<"Enter your choice (choose from 1 to 5): "<<endl;
int option;
cin>>option;
cout<<"Ente your two numbers separated by space: "<<endl;
cin>>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<<a<<endl;
break;
case 2:
cout<<b<<endl;
break;
case 3:
cout<<c<<endl;
break;
case 4:
cout<<d<<endl;
case 5:
cout<<e<<endl;
}

cout<<"Enter Y to continue the operation";
cin>>confirm;
}
while(confirm=='Y');
}


2017-08-28
satish comment

 satish

#include<iostream>
using namespace std;


int add(int a,int k)
{
int c;
c=a+k;
cout<<"Your Ans Is : "<<c;
}
int sub(int a,int k)
{
int c;
c=a-k;
cout<<"Your Ans Is : "<<c;
}
int mult(int a,int k)
{
int c;
c=a*k;
cout<<"Your Ans Is : "<<c;
}
int div(int a,int k)
{
int c;
c=a/k;
cout<<"Your Ans Is : "<<c;
}
int modual(int a,int k)
{
int c;
c=a%k;
cout<<"Your Ans Is : "<<c;
}

int main()
{
int a,b,c;
char s;
do
{
cout<<"1. Add"<<endl;
cout<<"2. Substraction"<<endl;
cout<<"3. Multipication"<<endl;
cout<<"4. Division"<<endl;
cout<<"5. moduals"<<endl;
cout<<endl<<"Please Choose One Option : ";cin>>a;
cout<<endl<<"Enter Two Value : ";cin>>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<<endl<<endl<<"Will You Continue .. Y/N :";cin>>s;
}while(s == 'Y'||s=='y');

}


2017-06-09
Alberth comment

 Alberth

Question 1
#include <iostream>
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 "<<AddCALL<<endl;}
else if (Num==2){
cout<<"Please enter 2 Numbers for your calculation"<<"\n";
cin>>Num1;
cin>>Num2;
RelCALL=Result(Num1,Num2);
cout<<"The Result is "<<RelCALL<<endl;
}
else if (Num==3){
cout<<"Please enter 2 Numbers for your calculation"<<"\n";
cin>>Num1;
cin>>Num2;

ProdCALL=Product(Num1,Num2);
cout<<"The Product is "<<ProdCALL<<endl;
}
else if(Num==4){cout<<"Please enter 2 Numbers for your calculation"<<"\n";
cin>>Num1;
cin>>Num2;


QuoCALL=Quotient(Num1,Num2);
cout<<"The Quocient is "<<QuoCALL<<endl;
}
else
{cout<<"Enter the number for the modlus operation: \n";
cin>>Num;
ModCALL=Mod(Num);
cout<<"The Modulus of the number entered is "<<ModCALL<<"\n";
}
cout<<"Do you have anymore Calculations to perform? \n";
cout<<"Enter 10 to continue or 11 to Exit: \n";
cin>>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
Alberth comment

 Alberth

Question 1
#include <iostream>
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 "<<AddCALL<<endl;}
else if (Num==2){
RelCALL=Result(Num1,Num2);
cout<<"The Result is "<<RelCALL<<endl;
}
else if (Num==3){
cout<<"Please enter 2 Numbers for your calculation"<<"\n";
cin>>Num1;
cin>>Num2;

ProdCALL=Product(Num1,Num2);
cout<<"The Product is "<<ProdCALL<<endl;
}
else if(Num==4){cout<<"Please enter 2 Numbers for your calculation"<<"\n";
cin>>Num1;
cin>>Num2;


QuoCALL=Quotient(Num1,Num2);
cout<<"The Quocient is "<<QuoCALL<<endl;
}
else
{cout<<"Enter the number for the modlus operation: \n";
cin>>Num;
ModCALL=Mod(Num);
cout<<"The Modulus of the number entered is "<<ModCALL<<"\n";
}
cout<<"Do you have anymore Calculations to perform? \n";
cout<<"Enter 10 to continue or 11 to Exit: \n";
cin>>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_Afify comment

 Ahmed_Afify

for exercise 1, this is my code:
#include <iostream>
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érez comment

 Ferran Pérez

For exercise 1, my solution was the following:

#include <iostream>
#include <math.h>
#include <cstdlib>
#include<iomanip>
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 comment

 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 <iostream>
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"<<result<<"\t"<<"\n";
}
int sub(int a,int b)
{
int result;
result=a-b;
cout<<"\t"<<"subtract:"<<"\t"<<result<<"\t"<<"\n";
}
int mul(int a,int b)
{
int result;
result=a*b;
cout<<"\t"<<"Multiplication:"<<"\t"<<result<<"\t"<<"\n";
}
int div(int a,int b)
{
int result;
result=a/b;
cout<<"\t"<<"Division:"<<"\t"<<result<<"\t"<<"\n";
}


2017-02-03
johnzkie comment

 johnzkie

soooooo harddddd c++ functions HUHU


2017-02-02
PELA comment

 PELA

THANKS


2016-11-30
aungaung comment

 aungaung

good


2016-11-29
habib adnan comment

 habib adnan

this side is a good


2016-11-03
bilal mughal comment

 bilal mughal

#include<iostream>
#include<conio.h>
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 "<<add1;
break;


case 2:

add2=sub();
cout<<"the sub of two number is "<<add2;
break;

case 3:

add3=mul();
cout<<"the mul of two number is "<<add3;
break;

case 4:

add4=div();
cout<<"the div of two number is "<<add4;
break;

case 5:

add5=mod();
cout<<"the modulus of two number is "<<add5;
break;

case 0:
cout<<"\nprogram terminated";
break;

default:
cout<<"invalid number!enter above option";



}
}while(num!=0);

getch();
return 0;

}


int add ()
{
int a,b;
cout<<"enter first value\n";
cin>>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 shariq comment

 mohd shariq

Hey guys ! Amazing website, helps too mu ch ,increase skills.


2016-04-11
Usman Shahzad comment

 Usman Shahzad

thankssss alo0ot..!! this website helps me alo0t... and also increase my skill..!!


2016-01-16
Junaid comment

 Junaid

very helpful for beginers like me


2015-12-26
pari comment

 pari

wow this website is really great.


2015-10-02
fc comment

 fc

coding is so fucking hard


2015-09-28
maic  comment

 maic

this website helps me to get more!!


2015-05-29
claude ndizeye comment

 claude ndizeye

how to use for loop,while,function,nested loop.


2015-05-12
Ahmad comment

 Ahmad

Thanks ...its help me alot,,,


2015-02-28
Charles Gira comment

 Charles Gira

its better to use this website!!!


2014-03-25



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.