Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Simple calculator v2
- #include<stdio.h>
- int main()
- {
- char c, str[20], snum1[20],snum2[20];
- int i,snum2length=0,num1,num2;
- printf("\n CALCULATOR (for positive integers only)\n\n");
- printf("\n Enter first number, operator (+,-,*,/) and second number: ");
- scanf("%s",str);
- i=0;
- while(isdigit(str[i]) && str[i]!='\0'){
- snum1[i]=str[i];
- i++;
- }
- snum1[i]='\0';
- num1=atoi(snum1);
- c=str[i];
- i++;
- while(isdigit(str[i]) && str[i]!='\0'){
- snum2[snum2length]=str[i];
- i++;
- snum2length++;
- }
- snum2[snum2length]='\0';
- num2=atoi(snum2);
- printf("\n %d %c %d = ",num1,c,num2);
- switch(c)
- {
- case '+':
- printf("%d \n",num1+num2);
- break;
- case '-':
- printf("%d \n",num1-num2);
- break;
- case '*':
- printf("%d \n",num1*num2);
- break;
- case '/':
- printf("%f \n",(float)num1/num2);
- break;
- default:
- printf("Error! Invalid Operator.");
- }
- printf("\n\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement