Calculator Code By using Functions

22:44 0 Comments A+ a-

Problem:
Write a program that should take two integer numbers and a character. Your program should also has following functions:
void sum(int, int); // takes two numbers as parameters and calculates and displays sum of the parameters
void diff(int, int); // takes two numbers as parameters and calculates and displays difference of the parameters
void product(int, int); // takes two numbers as parameters and calculates and displays product of the parameters
void division (int, int); // takes two numbers as parameters and calculates and displays quotient and remainder of the parameters
void power (int, int); // takes two numbers as parameters (base and exp) and calculates and displays base exp
your program should compare the character with the following values:

-if character is a ‘+’ then it should call sum function.
-if character is a ‘-’ then it should call diff function.
-if character is a ‘*’ then it should call product function.
-if character is a ‘/’ then it should call division function.
-if character is a ‘^’ then it should call power function.
Solution Code:
# include <iostream>
# include <cmath>
using namespace std;
void sum(int a,int b)
{
cout<<a+b<<endl;
}
void diff(int a,int b)
{
cout<<a-b<<endl;
}
void product(int a,int b)
{
cout<<a*b<<endl;
}
void division(int a,int b)
{
cout<<a/b<<endl;
}
void power(int a,int b)
{
cout<<pow(a,b)<<endl;
}

int main()
{
int a,b;
char oper;
cout<<"Please enter 1st integer : ";
cin>>a;
cout<<"Please enter 2nd integer : ";
cin>>b;
cout<<"Please enter one of the following operation sign as mentioned below"<<endl;
cout<<"Enter '+' for addition"<<endl;
cout<<"Enter '-' for subtraction"<<endl;
cout<<"Enter '*' for multiplication"<<endl;
cout<<"Enter '/' for division"<<endl;
cout<<"Enter '^' for taking power"<<endl;
cin>>oper;

if (oper=='+')
sum(a,b);
else if(oper =='-')
diff(a,b);
else if(oper =='*')
product(a,b);
else if(oper =='/')
division(a,b);
else if(oper =='^')
power(a,b);
else
cout<<"Operation entered is not valid... Try again"<<endl;


system("pause");
}