C++ – Calculate Sum, Div, Mul and Difference using Function

Write a program equivalent to a four-function calculator. The program should request a user to enter a number, an operator, and another number.
It should then carry out the specified arithmetical operation: adding, subtracting, dividing, or multiplying two numbers (Use the concept of function add(), sub(),…).
- C++ Functions to Add, Subtract, Multiply and Divide Using Operator Overloading
- C++ Program to Multiply Numbers Using Function Overloading
- Inline Function to Display the Net Payment to the Employee
Source Code,
/*program by using four function to calculate the sum, div, mul, and dif*/ #include<iostream.h> #include<conio.h> void sum(int x, char ch, int y); void dif(int x, char ch, int y); void div(int x, char ch, int y); void mul(int x, char ch, int y); void main() { int x, y; char ch; cout<<"Enter First Number:"; cin>>x; cout<<"Enter character:"; cin>>ch; cout<<"Enter Second Number:"; cin>>y; switch(ch) { case '+': { sum(x,ch,y); break; } case '-': { dif(x,ch,y); break; } case '/': { div(x,ch,y); break; } case'*': { mul(x,ch,y); break; } default: { cout<<"Wrong Choice"; break; } } getch(); } void sum(int x, char ch, int y) { int a; a=x+y; cout<<"Result is:"<<a; } void dif(int x, char ch, int y) { int d; d=x-y; cout<<"Result is:"<<d; } void div(int x, char ch, int y) { int di; di=x/y; cout<<"Result is:"<<di; } void mul(int x, char ch, int y) { int m; m=x*y; cout<<"Result is:"<<m; }
Output:
Enter First Number:3
Enter character:+
Enter Second Number:3
Result is:6