Write a program that lets the user perform arithmetic operations. First ask the
ID: 3678513 • Letter: W
Question
Write a program that lets the user perform arithmetic operations. First ask the user for a first number. Then use a while loop to do the following:
1. The user enters another second double number.
2. The user selects between four arithmetic operations (*,/,-,+).
3. Print the results of the operation of the first with the second number.
Keep the result and use it to as the first number the next time through the loop. Use a zero entered either at the first number or second number for quitting out of the app.
Required Methods: getOperation. addNumbers, mulitplyNumbers
Example run:
run:
Calculator App Enter first number (enter 0 to quit):
7
Enter second number (enter 0 to quit):
9
1 add
2 subtract
3 multiply
4 divide
1
The subtotal is: 16.0 Enter second number (enter 0 to quit):
4
1 add
2 subtract
3 multiply
4 divide
3
The subtotal is: 64.0 Enter second number (enter 0 to quit):
0
BUILD SUCCESSFUL (total time: 4 seconds)
Explanation / Answer
#include <iostream>
using namespace std;
void showChoices();
double add(double, double);
double subtract(double, double);
double multiply(double, double);
double divide(double, double);
int main()
{
double x,y;
int choice;
cout<< "enter first number";
cin >> x;
cout<< "enter second number";
cin >> y;
cout<< "enter your choice";
do
{
showChoices();
cin >> choice;
switch (choice)
{
case 1:
x=add(x,y);
cout << "Sum " << x <<endl;
cout << "enter second number";
cin >> y;
break;
case 2:
x=subtract(x,y);
cout << "Difference " << x <<endl;
cout << "enter second number";
cin >> y;
break;
case 3:
x=multiply(x,y);
cout << "Product " << x <<endl;
cout << "enter second number";
cin >> y;
break;
case 4:
x=divide(x,y);
cout << "Quotient " << x <<endl;
cout << "enter second number";
cin >> y;
break;
case 5:
break;
default:
cout << "Invalid choice" << endl;
}
}while (choice != 5);
return 0;
}
void showChoices()
{
cout << "MENU" << endl;
cout << "1: Add " << endl;
cout << "2: Subtract" << endl;
cout << "3: Multiply " << endl;
cout << "4: Divide " << endl;
cout << "5: Exit " << endl;
cout << "Enter your choice :";
}
double add(double a, double b)
{
return a+b;
}
double subtract(double a, double b)
{
return a-b;
}
double multiply(double a, double b)
{
return a*b;
}
double divide(double a, double b)
{
return a/b;
}
out put:
enter first number 4
enter second number 2
enter your choiceMENU
1: Add
2: Subtract
3: Multiply
4: Divide
5: Exit
Enter your choice :1
Sum 6
enter second number 3
MENU
1: Add
2: Subtract
3: Multiply
4: Divide
5: Exit
Enter your choice :4
Quotient 2
Program terminated (signal: SIGKILL)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.