Instructions:
Input
1. First number
2. Operator
3. Second number
Output
The first line will contain a message prompt to input the first number.
The second line will contain a message prompt to input the operator.
The third line will contain a message prompt to input the second number.
The last line contains the result with 2 decimal places.
Enter the first number: 5
Select the operator (+, -, *, /): +
Enter the second number: 0.70
Result = 5.70
#include <stdio.h>
int main() {
double x, y, res;
char op;
printf("Enter the first number: ");
scanf("%lf", &x);
getchar();
printf("Select the operator (+, -, *, /): ");
scanf("%c", &op);
getchar();
printf("Enter the second number: ");
scanf("%lf", &y);
switch (op) {
case '+':
res = x + y;
break;
case '-':
res = x - y;
break;
case '*':
res = x * y;
break;
case '/':
res = x / y;
break;
default:
printf("Unknown operator: %c\n", op);
return 0;
}
printf("Result = %.2lf\n", res);
return 0;
}
Comments
Leave a comment