C Program to Create Simple Calculator with Basic Operations
switch to select operations and scanf to get user input, like: switch(choice) { case '+': result = a + b; break; }.Examples
How to Think About It
switch or if statements to decide which operation to perform based on the operator. Finally, print the result or an error if the operation is invalid, like division by zero.Algorithm
Code
#include <stdio.h> int main() { double num1, num2, result; char op; printf("Enter first number: "); scanf("%lf", &num1); printf("Enter second number: "); scanf("%lf", &num2); printf("Enter operator (+, -, *, /): "); scanf(" %c", &op); switch(op) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': if(num2 != 0) result = num1 / num2; else { printf("Error! Division by zero.\n"); return 1; } break; default: printf("Invalid operator!\n"); return 1; } printf("Result: %.2lf\n", result); return 0; }
Dry Run
Let's trace the input 5, 3, and '+' through the code
Input numbers and operator
num1 = 5, num2 = 3, op = '+'
Switch on operator
case '+': result = 5 + 3 = 8
Print result
Output: Result: 8.00
| Step | Operation | Values |
|---|---|---|
| 1 | Input | num1=5, num2=3, op='+' |
| 2 | Calculate | result=8 |
| 3 | Output | Result: 8.00 |
Why This Works
Step 1: Getting user input
The program uses scanf to read two numbers and an operator from the user, storing them in variables.
Step 2: Choosing operation
A switch statement checks the operator and performs the matching arithmetic operation.
Step 3: Handling division by zero
Before dividing, the program checks if the second number is zero to avoid errors and prints an error message if so.
Step 4: Displaying the result
The result is printed with two decimal places using printf.
Alternative Approaches
#include <stdio.h> int main() { double a, b, res; char op; printf("Enter first number: "); scanf("%lf", &a); printf("Enter second number: "); scanf("%lf", &b); printf("Enter operator (+, -, *, /): "); scanf(" %c", &op); if(op == '+') res = a + b; else if(op == '-') res = a - b; else if(op == '*') res = a * b; else if(op == '/') { if(b != 0) res = a / b; else { printf("Error! Division by zero.\n"); return 1; } } else { printf("Invalid operator!\n"); return 1; } printf("Result: %.2lf\n", res); return 0; }
#include <stdio.h>
double add(double x, double y) { return x + y; }
double sub(double x, double y) { return x - y; }
double mul(double x, double y) { return x * y; }
double divi(double x, double y) { return y != 0 ? x / y : 0; }
int main() {
double num1, num2, result;
char op;
printf("Enter first number: ");
scanf("%lf", &num1);
printf("Enter second number: ");
scanf("%lf", &num2);
printf("Enter operator (+, -, *, /): ");
scanf(" %c", &op);
switch(op) {
case '+': result = add(num1, num2); break;
case '-': result = sub(num1, num2); break;
case '*': result = mul(num1, num2); break;
case '/':
if(num2 == 0) {
printf("Error! Division by zero.\n");
return 1;
}
result = divi(num1, num2);
break;
default:
printf("Invalid operator!\n");
return 1;
}
printf("Result: %.2lf\n", result);
return 0;
}Complexity: O(1) time, O(1) space
Time Complexity
The program performs a fixed number of operations regardless of input size, so it runs in constant time O(1).
Space Complexity
It uses a few variables for input and output, so space usage is constant O(1).
Which Approach is Fastest?
All approaches run in constant time; using functions adds slight overhead but improves code clarity.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Switch statement | O(1) | O(1) | Simple and clear operation selection |
| If-else statements | O(1) | O(1) | Easier for beginners to understand |
| Functions for operations | O(1) | O(1) | Better code organization and reuse |