0
0
Power-electronicsProgramBeginner · 2 min read

Embedded C Program for Simple Calculator

An Embedded C calculator program reads two numbers and an operator, then uses switch to perform addition, subtraction, multiplication, or division, printing the result with printf.
📋

Examples

Input5 + 3
OutputResult: 8
Input10 / 2
OutputResult: 5
Input7 * 0
OutputResult: 0
🧠

How to Think About It

To build a calculator in Embedded C, first get two numbers and an operator from the user. Then use 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

1
Get the first number from the user
2
Get the operator (+, -, *, /) from the user
3
Get the second number from the user
4
Use switch to check the operator and perform the corresponding operation
5
Print the result
6
Handle division by zero if operator is '/'
💻

Code

embedded_c
#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;
}
Output
Enter first number: 5 Enter operator (+, -, *, /): + Enter second number: 3 Result: 8.00
🔍

Dry Run

Let's trace the input '5 + 3' through the code

1

Input first number

num1 = 5

2

Input operator

op = '+'

3

Input second number

num2 = 3

4

Switch operation

case '+': result = 5 + 3 = 8

5

Print result

Output: Result: 8.00

StepVariableValue
1num15
2op+
3num23
4result8
5outputResult: 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

Using if-else instead of switch
embedded_c
#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;
}
This approach is more verbose but easier to read for beginners who are not familiar with switch.
Using functions for each operation
embedded_c
#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;
}
This method improves code organization and reusability by separating operations into functions.

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.

ApproachTimeSpaceBest For
Switch statementO(1)O(1)Simple and clear operation selection
If-else ladderO(1)O(1)Easier for beginners to understand
Functions for operationsO(1)O(1)Better code organization and reuse
💡
Always check for division by zero to avoid runtime errors in your calculator program.
⚠️
Beginners often forget to add a space before %c in scanf, causing input issues for the operator.