Suppose a company “ABC” is conducting a test to hire you as OOP Developer and asked you develop a calculator for the basic Arithmetic operators (+, -, *, /, %), where you have to deal at least 11 digit numbers. The numbers can be of with decimal point or without decimal point. The company wants you to develop an efficient code. You can choose any of the following options to develop this whole scenario:
You have to answer, which option will you choose and arguments in favour of your chosen option regarding code efficiency, code complexity and other factors.
#include <stdio.h>
int main() {
char op;
double num1, num2;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &op);
printf("Enter two operands: ");
scanf("%lf %lf", &num1, &num2);
switch (op) {
case '+':
printf("%.1lf + %.1lf = %.1lf", num1, num2, num1 + num2);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", num1, num2, num1 - num2);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", num1, num2, num1 * num2);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", num1, num2, num1 / num2);
break;
default:
printf("Error");
}
return 0;
}
Comments
Leave a comment