basic-c/sources/calculator.c

43 lines
977 B
C

#include <stdio.h>
int main (int argc, char *argv[]) {
float first;
float second;
float result;
int operator;
printf("Enter first operand: ");
scanf("%f", &first);
printf("/nEnter 1 for (+), 2 for (-), 3 for (*), 4 for (/): ");
scanf("%d", &operator);
if (operator == 4) {
do {
printf("/nEnter second operand: ");
scanf("%f", &second);
} while (second == 0);
} else {
printf("/nEnter second operand: ");
scanf("%f", &second);
}
switch (operator) {
case 1:
result = first + second;
break;
case 2:
result = first - second;
break;
case 3:
result = first * second;
break;
case 4:
result = first / second;
break;
default:
printf("Unknown operator\n");
return 1;
}
printf("Result is: %f \n", result);
return 0;
}