Write a C program called calculator.c that repeatedly allows a user to select +,
ID: 3634333 • Letter: W
Question
Write a C program called calculator.c that repeatedlyallows a user to select +, -, or * from a menu of operations
and two double operands. Then, your program should compute
the result of applying the selected operation to those operands.
In the program, you have to use only pointer variables and
dynamic memory.
A sample run of your program might look like this:
***************************
Operation Menu:
***************************
1. +
2. -
3. *
4. END
Select your choice: 1
Enter two numbers: 7.45 10.00
Result: 7.45 + 10.00 = 17.45
***************************
Operation Menu:
***************************
1. +
2. -
3. *
4. END
Select your choice: 4
BYE!
Explanation / Answer
Dear, /*This program display menu of choices and computes the selected operation*/ /*Header file for I/O operations*/ #include /*main function*/ int main() { /*variable declaration*/ float a,b; int ch=1; /*loop to display the menu and perform operations */ while (ch!=4) { /*display menu*/ printf("*************************** "); printf(" Operation Menu "); printf("*************************** "); printf(" 1. + "); printf(" 2. - "); printf(" 3. * "); printf(" 4. END "); printf("Select your choice: "); /*read the user choice*/ scanf("%d",&ch); /*switch case to do perform user choice of operation*/ switch(ch) { /*to perform addition*/ case 1: printf("Enter two numbers: "); scanf("%f%f", &a,&b); printf("Result: %.2f + %.2f = %.2f. ",a,b,(a+b)); break; /*to perform subtraction*/ case 2: printf("Enter two numbers: "); scanf("%f%f", &a,&b); printf("Result: %.2f + %.2f = %.2f. ",a,b,(a+b)); break; /*to perform multiplication*/ case 3: printf("Enter two numbers: "); scanf("%f%f", &a,&b); printf("Result: %.2f + %.2f = %.2f. ",a,b,(a+b)); break; /*to quit the program*/ case 4: printf("BYE!"); exit(0); /*to handle invalid choice selection*/ default: printf("Invalid Choice. Try gain... "); } } return 0; } Output: *************************** Operation Menu *************************** 1. + 2. - 3. * 4. END Select your choice: 1 Result: 7.45 + 10.00 = 17.45. *************************** Operation Menu *************************** 1. + 2. - 3. * 4. END Select your choice: 3 Enter two numbers: 5.00 6.03 Result: 5.00 + 6.03 = 11.03. *************************** Operation Menu *************************** 1. + 2. - 3. * 4. END Select your choice: 4 BYE! Hope this would help you.Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.