# Thread: calculator program

1. ## calculator program

I wrote my code for calculator

Code:
```#include<stdio.h>

int main ()
{
int op1;
int op2;
int result;
enum operator
{ plus, minus, multiply, divide}op;

printf("Enter first Number \n");
scanf("%d", &op1);

printf("Enter operator \n");
scanf("%d", &op);

printf("Enter second Number \n");
scanf("%d", &op2);

switch (op)
{
case plus :
result = (op1 + op2);
printf ("result : %d \n", result);
break;
case minus :
result = (op1 - op2);
printf ("result : %d \n", result);
break;

case multiply :
result = (op1 * op2);
printf ("result : %d \n", result);
break;
case divide :
result = (op1 / op2);
printf ("result : %d \n", result);
break;
}

return 0;
}```
Enter first Number
3
Enter operator
0
Enter second Number
2
result : 5

I do not see any problem in my code but can I make it better. mainly in operator I have to enter 0,1, 2, 3 2. > I do not see any problem in my code but can I make it better.

I believe not. It depends on what you want to "do" with your calculator program, what your target audience is and why do you think it's important to code a calculator. Also, if I were to be using such a calculator, I would like to type "1+1" or "1 + 1" instead of "1 0 1" to get the result. Think about how modern calculator apps tackle parsing problems. Maybe, coding a simple parser should be your next homework, if you'd like to do it.

Also, signed integers range from "2^-31" to "2^31 - 1" on most systems where size of integer is 4 bytes. What if I wanted to do some bigger computation, which in many cases of my chemistry equilibrium problems I need to? How would you tackle overflow/underflow issues without using Python or Java to make your calculator program?

That said, I'd like to add that this sort of program can be coded by any kid in their third grade too so long as they understand programming fundamentals and bare-minimum coding, so you should amp it up a bit and make coding a calculator a difficult task for yourself and dive into the probable issues that your program may face when end users would like to perform complex/tedious calculations, unless of-course you're a third grader ;) 3. Code:
```#include <stdio.h>
#include <stdbool.h>

bool calc()
{
int a, b, c = 0;
char o;
printf("> ");
if (scanf("%d %c %d", &a, &o, &b) != 3) return false;
switch (o)
{
case '+': c = a + b; break;
case '-': c = a - b; break;
case '*': c = a * b; break;
case '/': c = a / b; break;
case '%': c = a % b; break;
default:
printf("Unknown operator.\n");
return true;
}
printf("= %d\n", c);
return true;
}

int main()
{
printf("Numbers must be integers.\n"
"Operators: +, -, *, /, %%\n"
"Enter NUMBER OPERATOR NUMBER\n"
"Enter just q to quit.\n");

while (calc()) ;

return 0;
}``` Popular pages Recent additions #### Tags for this Thread

op2;, operator, printf, result, result; 