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?
1
Expert's answer
2014-08-13T11:02:38-0400
#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 the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS