Problem.
write a program to implement a calculator to perform all the four basic arithmetic operations on integers.
Solution.
#include <stdio.h>
int main() {
int operand1;
int operand2;
char operation;
scanf("%d %c %d", &operand1, &operation, &operand2);
// Addition
if (operation == '+') {
printf("Result: %d", operand1 + operand2);
}
// Subtraction
if (operation == '-') {
printf("Result: %d", operand1 - operand2);
}
// Multiplication
if (operation == '*') {
printf("Result: %d", operand1 * operand2);
}
// Division
if (operation == '/') {
printf("Result: %.3f", (float) operand1 / operand2);
}
return 0;
}Result
123 + 1223
Result: 1346
124 - 12
Result: 112
12 * 133
Result: 1596
143 / 24
Result: 5.958
http://www.AssignmentExpert.com/</stdio.h>
Comments