In: Computer Science
Write a C program with the following prompt *do not use goto statement* "Write an interactive program that implements a simple calculator. The program should allow the user to choose a binary arithmetic operation and enter two terms to which to apply the operation. The program should then compute the result and display it to the user. Your calculator will have two modes: double-precision mode and integer mode. Double-precision mode will do calculations and output using variables of type double. Integer mode will do calculations and output using variables of type int.Your program should start in double-precision mode."
#include
int main()
{
int op; //for storing choice of operator
double a,b,result; //for storing operands and result
int mode; //for storing the mode chosen
printf("Choose binary arithmetic operator: \n1. +\n2. *\n3. -\n4. /
\n5. %%\nEnter your choice: ");
scanf("%d",&op);
printf("\nEnter two operands: ");
scanf("%lf%lf",&a,&b);
switch(op) //evaluates the result based on op
{
case 1:result=a+b;
break;
case 3:result=a-b;
break;
case 4:result=a/b;
break;
case 2:result=a*b;
break;
case 5:result=(int)a%(int)b;
break;
}
//prompts for mode and prints the result
printf("\nSelect mode\n1.Double precision\n2.Integer : ");
scanf("%d",&mode);
if(mode==2)
printf("\nResult = %d",(int)result);
else printf("\nResult = %.2lf",result);
return 0;
}
//output screenshot