Write a calculator program using a do-while loop and a switch statement. You pro
ID: 3641441 • Letter: W
Question
Write a calculator program using a do-while loop and a switch
statement. You program should ask the user to select an operation
(+,-,*,/,q) and then it'll ask two floating point numbers as inputs.
It will respond in the following way:
* It computes and display the result if the operation is +, ?,*, / or
q. It quits without asking the user for further input if the operation
is 'q'.
* A blank character (space, tab or newline) is ignored. You can use
the library function isspace(char) to test if the character is a space
character. To use this function you must include the header file
<ctypes.h>
* If any other non-blank character is entered by the user the message
"Invalid operator" is printed and the user is not prompted to enter
two numbers.
Explanation / Answer
I DID IT WITH SWITCH, HOPE YOU UNDERSTAND #include #include int main() { char character,operation; float a,b ,res; do { printf("which opertion do you want to perform "); printf("type '+' for addition, '-' for subtraction, '*' for multiplication, '/' for division "); scanf(" %c ", &operation); switch (operation) { case '+': printf("enter the two value "); scanf("%f%f", &a,&b); res = a+b; printf("the answer is %f " ,res); break; case '-': printf("enter the two value "); scanf("%f%f", &a,&b); res = a-b; printf("the answer is %f " ,res); break; case '*': printf("enter the two value "); scanf("%f%f", &a,&b); res = a*b; printf("the answer is %d " ,res); break; case '/': printf("enter the two value "); scanf("%f%f", &a,&b); res = a/b; printf("the answer is %f" ,res); break; case 'q': exit(0); default: printf("invalid operation"); break; } } while(1); return (0); }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.