Write a C++ Program that contains four user defined function(s): addition(), subtraction(), division(), multiplication(). Develop a calculator as follows:
• In main() function:
o A menu with choices addition, subtraction, division and multiplication must be displayed.
o Get two numbers and a choice from user
o Call the respective functions with user given number as parameter using switch statement
o Print the result from addition (), subtraction (), division (), multiplication ().
• In user defined functions:
o Plus and minus function get two integer values and return integer.
o Multiply and Divide functions get two integer values and return float.
<
Answers
Answer:
#include <iostream>
#include <conio.h>
using namespace std;
int addition(int a, int b) {
int sum = a + b;
return sum;
}
int subraction(int a, int b) {
int difference = a - b;
return difference;
};
float multiplication(int a, int b) {
float product = float(a) * float(b);
return product;
};
float division(int a, int b) {
float quotient = float(a) / float(b);
return quotient;
};
int main() {
int opt; int a, b;
cout.width(50);
cout << "CALCULATOR\n";
cout << "1. Addition \n 2. Subraction \n 3.Multiplication \n 4.Division\n";
cout << "Enter option\n";
cin >> opt;
cout << "Enter Number 1:\t"; cin >> a;
cout << "Enter Number 2:\t"; cin >> b;
switch (opt)
{
case 1:cout << "\n The Sum Is : " << addition(a, b);
break;
case 2:cout << "\n The Difference Is :" << subraction(a,b);
break;
case 3:cout <<"\nThe Product Is :" << multiplication(a,b);
break;
case 4:cout << "\nThe Quotient Is :" <<division(a,b);
break;
default: cout << "Enter correct option";
break;
}
return 0;
}
Explanation:
Answer:
#include <iostream>
#include <conio.h>
using namespace std;
void addition(int, int);
void subtraction(int,int);
void division(double,double);
void multiplication(double, double);
void main()
{
cout << "-----------------Wellcome to My calculator------------------" << endl;
int a, b;
char ch;
cout << "Please Enter the first number :: ";
cin >> a;
cout << "Please Enter the seconed number :: ";
cin >> b;
cout <<"------------------------------------"<<endl<< "-------Choice any one opreter------- " << endl;
cout << "Enter the '+' for addition " << endl;
cout << "Enter the '-' for subtraction " << endl;
cout << "Enter the '/' for dividion " << endl;
cout << "Enter the '*' for multiplication " << endl;
cin >> ch;
cout << "------------------------------------" << endl;
switch (ch)
{
case '+':
addition(a, b);
break;
case '-':
subtraction(a, b);
break;
case '*':
multiplication(a, b);
break;
case '/':
division(a, b);
break;
default:
cout << "Invalid choice entered";
break;
}
cout << "------------------------------------" << endl;
cout << "Thank you for useing calculeter " << endl;
system("pause");
}
void addition(int num1, int num2)
{
int a;
a = num1 + num2;
cout << "your addition is :: " << a << endl;
}
void subtraction(int num1, int num2)
{
int a;
a = num1 - num2;
cout << "your subtraction is :: " << a << endl;
}
void division(double num1, double num2)
{
double a = double(num1)/double(num2);
cout << "your division is :: " << a << endl;
}
void multiplication(double num1, double num2)
{
double a = double(num1) * double(num2);
cout << "your multipilication is :: " << a << endl;
}
Explanation:
float replace with double