60 525
Assignments Done
98,1%
Successfully Done
In April 2018

Answer to Question #44893 in C for erico fire

Question #44893
write a c program for a simple calculator the system allows the user to choose the operator the she wants to use after the user has entered the 2 numbers the system must display the final answer?
Expert's answer
#include <stdio.h>
int main() {
int a, b, res;
char op;
printf("Input first number: ");
scanf("%d", &a); // read first number
while (getchar() != '\n'); // skip all unread characters
printf("Input second number: ");
scanf("%d", &b); // read second number
while (getchar() != '\n'); // skip all unread characters
printf("Input the operation(+,-,*,/): ");
op = getchar(); // read the character of operation
switch (op) {
case '+': 
res = a + b;
break;
case '-': 
res = a - b;
break;
case '*': 
res = a * b;
break;
case '/': 
res = (float) a / b;
break;
default:
printf("Invalid operator\n");
return;
}
printf("%d %c %d = %d\n", a, op, b, res);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions