C Program to Make a Simple Calculator Using switch…case

Creating a simple calculator using switch...case in C is an excellent way to understand how decision-making constructs work in C programming. This article will cover various C Program to Make a Simple Calculator Using switch…case each demonstrating different ways to use switch...case. We will include real examples with different solutions and outputs.

Prerequisites

Before diving into the examples, ensure you have the following prerequisites:

  • Basic understanding of C programming.
  • Familiarity with decision-making constructs (switch...case).
  • A C compiler installed on your system (e.g., GCC).

Simple Calculator

1.1 Explanation

A simple calculator performs basic arithmetic operations such as addition, subtraction, multiplication, and division. The user will input two numbers and an operator, and the program will display the result based on the chosen operator.

1.2 Program: Basic Arithmetic Calculator

This program performs basic arithmetic operations using switch...case.

C
#include <stdio.h>

int main() {
    char operator;
    double first, second;

    printf("Enter an operator (+, -, *, /): ");
    scanf("%c", &operator);
    printf("Enter two operands: ");
    scanf("%lf %lf", &first, &second);

    switch (operator) {
    case '+':
        printf("%.1lf + %.1lf = %.1lf\n", first, second, first + second);
        break;
    case '-':
        printf("%.1lf - %.1lf = %.1lf\n", first, second, first - second);
        break;
    case '*':
        printf("%.1lf * %.1lf = %.1lf\n", first, second, first * second);
        break;
    case '/':
        if (second != 0.0)
            printf("%.1lf / %.1lf = %.1lf\n", first, second, first / second);
        else
            printf("Error! Division by zero.\n");
        break;
    default:
        printf("Error! Operator is not correct.\n");
    }

    return 0;
}

1.3 Output

C
Enter an operator (+, -, *, /): +
Enter two operands: 5 3
5.0 + 3.0 = 8.0

Advanced Calculator

2.1 Explanation

An advanced calculator can include additional operations like modulus and power. This example demonstrates how to expand the functionality of the basic calculator.

2.2 Program: Advanced Arithmetic Calculator

This program includes additional operations such as modulus and power.

C
#include <stdio.h>
#include <math.h>

int main() {
    char operator;
    double first, second;

    printf("Enter an operator (+, -, *, /, %, ^): ");
    scanf(" %c", &operator);
    printf("Enter two operands: ");
    scanf("%lf %lf", &first, &second);

    switch (operator) {
    case '+':
        printf("%.1lf + %.1lf = %.1lf\n", first, second, first + second);
        break;
    case '-':
        printf("%.1lf - %.1lf = %.1lf\n", first, second, first - second);
        break;
    case '*':
        printf("%.1lf * %.1lf = %.1lf\n", first, second, first * second);
        break;
    case '/':
        if (second != 0.0)
            printf("%.1lf / %.1lf = %.1lf\n", first, second, first / second);
        else
            printf("Error! Division by zero.\n");
        break;
    case '%':
        if ((int)second != 0)
            printf("%d %% %d = %d\n", (int)first, (int)second, (int)first % (int)second);
        else
            printf("Error! Division by zero.\n");
        break;
    case '^':
        printf("%.1lf ^ %.1lf = %.1lf\n", first, second, pow(first, second));
        break;
    default:
        printf("Error! Operator is not correct.\n");
    }

    return 0;
}

2.3 Output

C
Enter an operator (+, -, *, /, %, ^): ^
Enter two operands: 2 3
2.0 ^ 3.0 = 8.0

Menu-Driven Calculator

3.1 Explanation

A menu-driven calculator provides a user-friendly way to select operations from a menu. The program repeatedly displays the menu until the user decides to exit.

3.2 Program: Menu-Driven Calculator

This program demonstrates a menu-driven approach to using switch...case.

C
#include <stdio.h>
#include <math.h>

void menu() {
    printf("\nSelect an operation:\n");
    printf("1. Addition\n");
    printf("2. Subtraction\n");
    printf("3. Multiplication\n");
    printf("4. Division\n");
    printf("5. Modulus\n");
    printf("6. Power\n");
    printf("7. Exit\n");
}

int main() {
    int choice;
    double first, second;

    do {
        menu();
        printf("Enter your choice: ");
        scanf("%d", &choice);

        if (choice >= 1 && choice <= 6) {
            printf("Enter two operands: ");
            scanf("%lf %lf", &first, &second);
        }

        switch (choice) {
        case 1:
            printf("%.1lf + %.1lf = %.1lf\n", first, second, first + second);
            break;
        case 2:
            printf("%.1lf - %.1lf = %.1lf\n", first, second, first - second);
            break;
        case 3:
            printf("%.1lf * %.1lf = %.1lf\n", first, second, first * second);
            break;
        case 4:
            if (second != 0.0)
                printf("%.1lf / %.1lf = %.1lf\n", first, second, first / second);
            else
                printf("Error! Division by zero.\n");
            break;
        case 5:
            if ((int)second != 0)
                printf("%d %% %d = %d\n", (int)first, (int)second, (int)first % (int)second);
            else
                printf("Error! Division by zero.\n");
            break;
        case 6:
            printf("%.1lf ^ %.1lf = %.1lf\n", first, second, pow(first, second));
            break;
        case 7:
            printf("Exiting...\n");
            break;
        default:
            printf("Invalid choice! Please select a valid option.\n");
        }
    } while (choice != 7);

    return 0;
}

3.3 Output

C
Select an operation:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
6. Power
7. Exit
Enter your choice: 1
Enter two operands: 5 3
5.0 + 3.0 = 8.0

Select an operation:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
6. Power
7. Exit
Enter your choice: 7
Exiting...

Conclusion

In this article, we explored a C Program to Make a Simple Calculator Using switch…case. We covered basic arithmetic operations, an advanced calculator with additional operations, and a menu-driven calculator. By practicing these examples, you will gain a solid understanding of how to use switch...case for decision-making in C programming