Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How do you make a calculator

+11 votes
asked 4 days ago by STEPHEN OLATUNDE OLORUNJOBA (240 points)

1 Answer

+1 vote
answered 10 hours ago by Yatharath Jindal (160 points)
#include <stdio.h>

int main() {
    char operator;
    double num1, num2, result;

    // Step 1: Request the mathematical operator from the user
    printf("Enter an operator (+, -, *, /): ");
    scanf("%c", &operator);

    // Step 2: Request the two numeric operands
    printf("Enter two numbers: ");
    scanf("%lf %lf", &num1, &num2);

    // Step 3: Evaluate the operator and compute the result
    switch (operator) {
        case '+':
            result = num1 + num2;
            printf("%.2lf + %.2lf = %.2lf\n", num1, num2, result);
            break;

        case '-':
            result = num1 - num2;
            printf("%.2lf - %.2lf = %.2lf\n", num1, num2, result);
            break;

        case '*':
            result = num1 * num2;
            printf("%.2lf * %.2lf = %.2lf\n", num1, num2, result);
            break;

        case '/':
            // Edge case validation: avoid division by zero
            if (num2 != 0.0) {
                result = num1 / num2;
                printf("%.2lf / %.2lf = %.2lf\n", num1, num2, result);
            } else {
                printf("Error! Division by zero is not allowed.\n");
            }
            break;

        // Triggers if the user types an invalid math symbol
        default:
            printf("Error! Invalid operator entered.\n");
    }

    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...