Coding Simplicity : C Program for Simple Calculator

C Program for Simple Calculator

Start a fun and interesting journey into coding by using our easy-to-follow tutorial on  C program for simple calculator. All operations—multiplying, dividing, adding, and subtracting—are accessible at your fingertips. This course is designed for beginners and will help you create a basic but functional calculator which is user driven.

Our instructions simplify the process for coding newcomers. You don’t need an extensive understanding of numbers to enjoy this fun, hands-on trip. Come join us and discover the joy of learning to code! Together, let’s make it both enjoyable and practical!

Algorithm

Start:
     Begin the program execution.
Declare Variables:
     Set the operator, result, operand1, and operand2 variables. 
Input:
     Request that the operator, operand1, and operand2 be entered by the user.
Perform Calculation:
     Use if-else statements to check the operator.
     If the operator is '+', add operand1 and operand2.
     If the operator is '-', subtract operand2 from operand1.
     If the operator is '*', multiply operand1 and operand2.
     If the operator is '/', divide operand1 by operand2.
Display Result:
     Show the calculated result.
End:
     End the program.

Pseudocode

// Declare variables
float operand1, operand2, result;
char operation;

// Input
printf("Enter operand1: ");
scanf("%f", &operand1);

printf("Enter operation (+, -, *, /): ");
scanf(" %c", &operation);

printf("Enter operand2: ");
scanf("%f", &operand2);

// Perform Calculation
if (operation == '+') {
    result = operand1 + operand2;
} else if (operation == '-') {
    result = operand1 - operand2;
} else if (operation == '*') {
    result = operand1 * operand2;
} else if (operation == '/') {
    // Check for division by zero
    if (operand2 != 0) {
        result = operand1 / operand2;
    } else {
        printf("Error: Division by zero\n");
        return 1; // Exit with error code
    }
} else {
    printf("Error: Invalid operation\n");
    return 1; // Exit with error code
}

// Display Result
printf("Final Output: %f\n", result);

Various Methods for Writing a simple Calculator Program in C

The different methods available for creating a calculator program in the C language are presented below.

  1. C Program For Simple Calculator Using Switch Statement.
  2. C program for simple calculator using if else if statement.
  3. C calculator program with a switch statement and do-while loop.
  4. C program for simple calculator using functions with Switch Statement.

Example 1 : C Program For Simple Calculator Using Switch Statement

In this case, we choose the switch command to find the function and do the calculation. I like how it simplifies and organizes handling various cases.

#include <stdio.h>

int main() {
    float operand1, operand2, result;
    char operator;

    printf("Enter operand1: ");
    scanf("%f", &operand1);

    printf("Enter operator (+, -, *, /): ");
    scanf(" %c", &operator);

    printf("Enter operand2: ");
    scanf("%f", &operand2);

    switch (operator) {
        case '+':
            result = operand1 + operand2;
            break;
        case '-':
            result = operand1 - operand2;
            break;
        case '*':
            result = operand1 * operand2;
            break;
        case '/':
            if (operand2 != 0) {
                result = operand1 / operand2;
            } else {
                printf("Error: Division by zero\n");
                return 1;
            }
            break;
        default:
            printf("Error: Invalid operator\n");
            return 1;
    }

    printf("Final Output: %.2f\n", result);

    return 0;
}

Example 2 : C program for simple calculator using if else if statement

We decided to keep things simple by using if-else lines to find the operator and carry out the action that goes with it. This method guarantees that everyone can code the calculator with ease.

#include <stdio.h>

int main() {
    float operand1, operand2, result;
    char operator;

    printf("Enter operand1: ");
    scanf("%f", &operand1);

    printf("Enter operator (+, -, *, /): ");
    scanf(" %c", &operator);

    printf("Enter operand2: ");
    scanf("%f", &operand2);

    if (operator == '+') {
        result = operand1 + operand2;
    } else if (operator == '-') {
        result = operand1 - operand2;
    } else if (operator == '*') {
        result = operand1 * operand2;
    } else if (operator == '/') {
        if (operand2 != 0) {
            result = operand1 / operand2;
        } else {
            printf("Error: Division by zero\n");
            return 1;
        }
    } else {
        printf("Error: Invalid operator\n");
        return 1;
    }

    printf("Final Output: %.2f\n", result);

    return 0;
}

Example 3 :Calculator Program in C using do-while loop and switch statement

In this implementation, a do-while loop allows the user to perform multiple calculations until they choose to exit the program. The switch statement handles the operator selection.

#include <stdio.h>

int main() {
    char choice;
    do {
        float operand1, operand2, result;
        char operator;

        printf("Enter operand1: ");
        scanf("%f", &operand1);

        printf("Enter operator (+, -, *, /): ");
        scanf(" %c", &operator);

        printf("Enter operand2: ");
        scanf("%f", &operand2);

        switch (operator) {
            case '+':
                result = operand1 + operand2;
                break;
            case '-':
                result = operand1 - operand2;
                break;
            case '*':
                result = operand1 * operand2;
                break;
            case '/':
                if (operand2 != 0) {
                    result = operand1 / operand2;
                } else {
                    printf("Error: Division by zero\n");
                    return 1;
                }
                break;
            default:
                printf("Error: Invalid operator\n");
                return 1;
        }

        printf("Final Output: %.2f\n", result);

        printf("Do you wish to execute a different calculation? (y/n): ");
        scanf(" %c", &choice);

    } while (choice == 'y' || choice == 'Y');

    return 0;
}

Example 4 :Calculator Program in C using function and switch statement

This implementation enhances modularity by encapsulating the arithmetic operations in separate functions. The switch statement handles the operator selection, and each function corresponds to a specific operation.

#include <stdio.h>

// Function declarations
float add(float a, float b) {
    return a + b;
}

float subtract(float a, float b) {
    return a - b;
}

float multiply(float a, float b) {
    return a * b;
}

float divide(float a, float b) {
    if (b != 0) {
        return a / b;
    } else {
        printf("Error: Division by zero\n");
        return 0;
    }
}

int main() {
    float operand1, operand2, result;
    char operator;

    printf("Enter operand1: ");
    scanf("%f", &operand1);

    printf("Enter operator (+, -, *, /): ");
    scanf(" %c", &operator);

    printf("Enter operand2: ");
    scanf("%f", &operand2);

    switch (operator) {
        case '+':
            result = add(operand1, operand2);
            break;
        case '-':
            result = subtract(operand1, operand2);
            break;
        case '*':
            result = multiply(operand1, operand2);
            break;
        case '/':
            result = divide(operand1, operand2);
            break;
        default:
            printf("Error: Invalid operator\n");
            return 1;
    }

    printf("Final Output: %.2f\n", result);

    return 0;
}

These different approaches to creating a calculator program in C cater to various coding preferences and scenarios, providing flexibility in implementation based on specific requirements.

Conclusion

This article taught us the fundamentals of creating C code and helped us design an intuitive calculator. No more problems now with addition, subtraction, multiplication, or division.

Like an apartment organization plan, functions and if-then statements aren’t just fancy words; they’re also tools that assist us make our code simpler for us to understand.

We’ve also added error checks to make the experience smooth. We’ll make sure the process goes and without any stress, whether you need to divide by zero or deal with an unknown number.

if you want to read more on switch case in c, click here.

Leave a Reply

Your email address will not be published. Required fields are marked *