Write a C++ Program that simulates a basic calculator using functions which perf
ID: 3776943 • Letter: W
Question
Write a C++ Program that simulates a basic calculator using functions which performs the operations of Addition, Subtraction, multiplication, and Division. ( make sure you cover the case to avoid division by a zero)
1)Display a menu for list of operations that can be calculated and get the input from user about his choice of calculation.
2) Based on user choice of operation, take the input of number/numbers from user. Assume all input values are of type double.
3)Calculations must be implemented using functions which return a double value.
4)Result of the calculation should be displayed on the screen
Explanation / Answer
#include<iostream.h>
#include<conio.h>
double add(double a, double b)
{
return a+b;
}
double sub(double a, double b)
{
return a-b;
}
double mult(double a, double b)
{
return a*b;
}
double div(double a, double b)
{
if(b==0)
return -1;
return a/b;
}
void main()
{
clrscr();
double a, b;
int ch;
cout<<"1. Addition"<<endl;
cout<<"2. Subtraction"<<endl;
cout<<"3.Multiplication"<<endl;
cout<<"4.Division"<<endl;
cout<<"Enter your choice: ";
cin>>ch;
cout<<"Enter value 1:";
cin>>a;
cout<<"Enter value 2:";
cin>>b;
if(ch==4 && b == 0)
cout<<"Error. Division by 0";
else{
if(ch==1)
cout<<"Result: "<<add(a,b);
else if(ch==2)
cout<<"Result: "<<sub(a,b);
else if(ch==3)
cout<<"Result: "<<mult(a,b);
else if(ch==4)
cout<<"Result: "<<div(a,b);
else
cout<<"Invalid choice";
}
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.