Design a calculator program that will add, subtract, multiply, or divide two num
ID: 3639217 • Letter: D
Question
Design a calculator program that will add, subtract, multiply, or divide two numbers input by a user.Your program design should contain the following:
* The main menu of your program is to continue to prompt the user for an arithmetic choice until the user enters a sentinel value to quit the calculator program.
* When the user chooses an arithmetic operation (i.e. addition) the operation is to continue to be performed (i.e. prompting the user for each number, displaying the result, prompting the user to add two more different numbers) until the user enters a sentinel value to end the chosen arithmetic operation.
* If the user chooses division, do not allow the user to divide by 0. Display an error message to user and ask the user to choose another denominator.
Explanation / Answer
#include
# include
int main()
{
int sum, difference, product, quotient;
int count = 0;
int num1, num2;
char choice, choice2;
do
{
printf( “ Welcome to My Calculator!!! ”);
printf(“ A. Addition:”);
printf(“ B. Subtraction:”);
printf(“ C. Multiplication:”);
printf(“ D. Division:”);
printf(“ Q. Quit:”);
printf(“ Please enter your choice (A/B/C/D/Q):”;
scanf(“%c”,&choice);
if(choice == ‘Q’ || choice == ‘q’)
{
printf(“Exiting from calculator!!!”);
exit(0);
}
else
{
if(choice == ‘A’ || choice == ‘a’)
{
sum = 0;
do
{
printf(“Enter the 1st number:”);
scanf(“%d”,&num1);
printf(“Enter the 2ndnumber:”);
scanf(“%d”,&num2);
sum+ = num1 + num2;
printf(“Do you wish to add more to this? (y/n)”);
scanf(“%c,choice2);
}while(choice2 == ‘Y’ || choice2 == ‘y’);
}
else
{
if(choice == ‘B’ || choice == ‘b’)
{
difference = 0;
do
{
printf(“Enter the 1st number:”);
scanf(“%d”,&num1);
printf(“Enter the 2ndnumber:”);
scanf(“%d”,&num2);
difference- = num1 - num2;
printf(“Do you wish to subtract more from this? (y/n)”);
scanf(“%c,choice2);
}while(choice2 == ‘Y’ || choice2 == ‘y’);
}
else
{
if(choice == ‘C’ || choice == ‘c’)
{
product = 0;
do
{
printf(“Enter the 1st number:”);
scanf(“%d”,&num1);
printf(“Enter the 2ndnumber:”);
scanf(“%d”,&num2);
product+*= num1 * num2;
printf(“Do you wish to multiply more to this? (y/n)”);
scanf(“%c,choice2);
}while(choice2 == ‘Y’ || choice2 == ‘y’);
}
else
{
if(choice == ‘D’ || choice == ‘d’)
{
quotient = 1;
do
{
count++;
printf(“Enter the 1st number:”);
scanf(“%d”,&num1);
printf(“Enter the 2ndnumber:”);
scanf(“%d”,&num2);
if(num2 == 0)
{
while(num2 == 0)
{
printf(“Enter the 2ndnumber:”);
scanf(“%d”,&num2);
}
}
if(count == 1)
{
quotient = num1 / num2;
}
else
{
quotient = quotient/ (num1/num2);
}
printf(“Do you wish to divide more to this? (y/n)”);
scanf(“%c,choice2);
}while(choice2 == ‘Y’ || choice2 == ‘y’);
}
}
}
}
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.