A simple C program that uses functions to create a basic calculator

List Topics
July 10, 2024
No Comments
4 min read

A simple C program that uses functions to create a basic calculator that can perform addition, subtraction, multiplication, and division

C
#include <stdio.h>

// Function prototypes
void add();
void subtract();
void multiply();
void divide();

int main() {
    int choice;
    
    do {
        printf("\nSimple Calculator\n");
        printf("1. Addition\n");
        printf("2. Subtraction\n");
        printf("3. Multiplication\n");
        printf("4. Division\n");
        printf("5. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        
        switch (choice) {
            case 1:
                add();
                break;
            case 2:
                subtract();
                break;
            case 3:
                multiply();
                break;
            case 4:
                divide();
                break;
            case 5:
                printf("Exiting...\n");
                break;
            default:
                printf("Invalid choice. Please try again.\n");
        }
    } while (choice != 5);
    
    return 0;
}

// Function definitions
void add() {
    double num1, num2;
    printf("Enter two numbers: ");
    scanf("%lf %lf", &num1, &num2);
    printf("Result: %.2lf\n", num1 + num2);
}

void subtract() {
    double num1, num2;
    printf("Enter two numbers: ");
    scanf("%lf %lf", &num1, &num2);
    printf("Result: %.2lf\n", num1 - num2);
}

void multiply() {
    double num1, num2;
    printf("Enter two numbers: ");
    scanf("%lf %lf", &num1, &num2);
    printf("Result: %.2lf\n", num1 * num2);
}

void divide() {
    double num1, num2;
    printf("Enter two numbers: ");
    scanf("%lf %lf", &num1, &num2);
    if (num2 != 0) {
        printf("Result: %.2lf\n", num1 / num2);
    } else {
        printf("Error: Division by zero is not allowed.\n");
    }
}

Explanation:

  1. Function Prototypes: The function prototypes for add, subtract, multiply, and divide are declared at the beginning.
  2. Main Function: The main function presents a menu to the user and performs the chosen operation using a switch statement. The menu repeats until the user chooses to exit (choice 5).
  3. Operation Functions: Each operation function (add, subtract, multiply, divide) prompts the user for two numbers, performs the operation, and displays the result. The division function includes a check to prevent division by zero.

Another Way

simple calculator program that uses a single function to handle all the arithmetic operations. This approach reduces redundancy and makes the code more modular.

C
#include <stdio.h>

// Function prototypes
void performOperation(char operation);

int main() {
    int choice;
    
    do {
        printf("\nSimple Calculator\n");
        printf("1. Addition\n");
        printf("2. Subtraction\n");
        printf("3. Multiplication\n");
        printf("4. Division\n");
        printf("5. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        
        switch (choice) {
            case 1:
                performOperation('+');
                break;
            case 2:
                performOperation('-');
                break;
            case 3:
                performOperation('*');
                break;
            case 4:
                performOperation('/');
                break;
            case 5:
                printf("Exiting...\n");
                break;
            default:
                printf("Invalid choice. Please try again.\n");
        }
    } while (choice != 5);
    
    return 0;
}

// Function definition
void performOperation(char operation) {
    double num1, num2, result;
    printf("Enter two numbers: ");
    scanf("%lf %lf", &num1, &num2);
    
    switch (operation) {
        case '+':
            result = num1 + num2;
            printf("Result: %.2lf\n", result);
            break;
        case '-':
            result = num1 - num2;
            printf("Result: %.2lf\n", result);
            break;
        case '*':
            result = num1 * num2;
            printf("Result: %.2lf\n", result);
            break;
        case '/':
            if (num2 != 0) {
                result = num1 / num2;
                printf("Result: %.2lf\n", result);
            } else {
                printf("Error: Division by zero is not allowed.\n");
            }
            break;
        default:
            printf("Invalid operation.\n");
    }
}

Explanation:

  1. Function Prototypes: The function prototype for performOperation is declared at the beginning.
  2. Main Function: The main function presents a menu to the user and calls performOperation with the appropriate operation character based on the user's choice. The menu repeats until the user chooses to exit (choice 5).
  3. performOperation Function: This function takes a character (operation) as an argument, which specifies the arithmetic operation to perform. It prompts the user for two numbers, performs the specified operation, and displays the result. The division operation includes a check to prevent division by zero.
©2025 Linux Bangla | Developed & Maintaind by Linux Bangla.