Create a calculator program that works as follows: 1. Your program should read a
ID: 3535060 • Letter: C
Question
Create a calculator program that works as follows:
1. Your program should read an integer, an arithmetic operation, and another integer in that
order.
2. Valid arithmetic operations are+,*,-, and/.
3. Output will depend on which arithmetic operation is performed:
+ Print the sum of the numbers.
* Print the product of the numbers.
− Print the second number subtracted from the ï¬rst.
/ Print the ï¬rst number divided by the second.
4. Use integer arithmetic only.
Example 1:
$ ./a
9 * 3
27
Example 2:
$ ./a
9 - 3
6
Explanation / Answer
#include<stdio.h>
#include<string.h>
int main()
{
char s[310];
int a,o,b;
int temp=0;
printf("enter the operatoion");
fgets(s,310,stdin);
if(s[1]=='*' || s[1]=='+' ||s[1]=='/' || s[1]=='-')
{ //printf("%d ",s[2]);
a=s[0]-'0';
b=s[2]-'0';
printf("%d ",a);
printf("%d ",b);
if(s[1]=='*')
{ temp=temp+a*b;
printf("%d",temp);
}
if(s[1]=='+')
{ temp=temp+a+b;
printf("%d",temp);
}
if(s[1]=='-')
{ temp=temp+a-b;
printf("%d",temp);
}
if(s[1]=='/')
{ temp=temp+a/b;
printf("%d",temp);
}
}
else
{
printf("error in operator");
}
}