Embedded C Program for Simple Calculator
switch to perform addition, subtraction, multiplication, or division, printing the result with printf.Examples
How to Think About It
switch to decide which math operation to do based on the operator. Finally, print the result. This approach keeps the program simple and easy to follow.Algorithm
Code
#include <stdio.h> int main() { float num1, num2, result; char op; printf("Enter first number: "); scanf("%f", &num1); printf("Enter operator (+, -, *, /): "); scanf(" %c", &op); printf("Enter second number: "); scanf("%f", &num2); 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: %.2f\n", result); return 0; }
Dry Run
Let's trace the input '5 + 3' through the code
Input first number
num1 = 5
Input operator
op = '+'
Input second number
num2 = 3
Switch operation
case '+': result = 5 + 3 = 8
Print result
Output: Result: 8.00
| Step | Variable | Value |
|---|---|---|
| 1 | num1 | 5 |
| 2 | op | + |
| 3 | num2 | 3 |
| 4 | result | 8 |
| 5 | output | Result: 8.00 |
Why This Works
Step 1: Reading inputs
The program uses scanf to get two numbers and an operator from the user, storing them in variables.
Step 2: Choosing operation
It uses a switch statement on the operator to decide which math operation to perform.
Step 3: Handling division
Before dividing, it checks if the second number is zero to avoid errors, printing an error message if so.
Step 4: Displaying result
Finally, it prints the result formatted to two decimal places using printf.
Alternative Approaches
#include <stdio.h> int main() { float num1, num2, result; char op; printf("Enter first number: "); scanf("%f", &num1); printf("Enter operator (+, -, *, /): "); scanf(" %c", &op); printf("Enter second number: "); scanf("%f", &num2); if(op == '+') result = num1 + num2; else if(op == '-') result = num1 - num2; else if(op == '*') result = num1 * num2; else if(op == '/') { if(num2 != 0) result = num1 / num2; else { printf("Error: Division by zero\n"); return 1; } } else { printf("Invalid operator\n"); return 1; } printf("Result: %.2f\n", result); return 0; }
#include <stdio.h> float add(float a, float b) { return a + b; } float sub(float a, float b) { return a - b; } float mul(float a, float b) { return a * b; } float divi(float a, float b) { return b != 0 ? a / b : 0; } int main() { float num1, num2, result; char op; printf("Enter first number: "); scanf("%f", &num1); printf("Enter operator (+, -, *, /): "); scanf(" %c", &op); printf("Enter second number: "); scanf("%f", &num2); switch(op) { case '+': result = add(num1, num2); break; case '-': result = sub(num1, num2); break; case '*': result = mul(num1, num2); break; case '/': if(num2 != 0) result = divi(num1, num2); else { printf("Error: Division by zero\n"); return 1; } break; default: printf("Invalid operator\n"); return 1; } printf("Result: %.2f\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 O(1) time; using functions adds slight overhead but improves readability.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Switch statement | O(1) | O(1) | Simple and clear operation selection |
| If-else ladder | O(1) | O(1) | Easier for beginners to understand |
| Functions for operations | O(1) | O(1) | Better code organization and reuse |