1. Write a C program to simulate a simple calculator.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
int iChoice, iOperand1, iOperand2;
char cOperator;
for(;;)
{
printf("\nEnter the arithmetic expression(Do not add spaces in the
expression)\n");
scanf("%d%c%d", &iOperand1, &cOperator, &iOperand2);
switch(cOperator)
{
case ’+’: printf("\nResult = %d", iOperand1 + iOperand2);
break;
case ’-’: printf("\nResult = %d", iOperand1 - iOperand2);
break;
case ’*’: printf("\nResult = %d", iOperand1 * iOperand2);
break;
case ’/’: printf("\nResult = %g", (float)iOperand1 / iOperand2);
break;
case ’%’: printf("\nResult = %d", iOperand1 % iOperand2);
break;
}
printf("\nPress 1 to continue and 0 to quit : ");
scanf("%d", &iChoice);
if(0==iChoice)
{
break;
}
}
return 0;
}
/Output
=================================
***************************************
Enter the arithmetic expression
4+6
Result = 10
Press 1 to continue and 0 to quit : 1
Enter the arithmetic expression
2-9
Result = -7
Press 1 to continue and 0 to quit : 1
Enter the arithmetic expression
5*2
Result = 10
Press 1 to continue and 0 to quit : 1
Enter the arithmetic expression
4/5
Result = 0.8
Press 1 to continue and 0 to quit : 1
Enter the arithmetic expression
8/4
Result = 2
Press 1 to continue and 0 to quit : 1
Enter the arithmetic expression
15%4
Result = 3
Press 1 to continue and 0 to quit : 0
***************************************/