C Program to Make a Simple Calculator Using switch…case

Created with Sketch.

C Program to Make a Simple Calculator Using switch…case

In this tutorial, we’ll develop a simple calculator program in C using the switch statement. This calculator will perform basic arithmetic operations such as addition, subtraction, multiplication, and division. We’ll design an algorithm to handle user input and implement the calculator functionality using a switch statement. Additionally, we’ll provide examples of the program’s output for better understanding.

Algorithm for Simple Calculator Program

  1. Input: Accept two operands and an operator from the user.
  2. Menu Display: Present the user with a menu of operations (addition, subtraction, multiplication, division).
  3. Switch…Case Structure:
    • Use a switch statement to select the operation based on the user’s choice.
    • Perform the corresponding arithmetic operation and display the result.
    • Handle division by zero to avoid runtime errors.
  4. Repeat or Exit: Ask the user if they want to perform another calculation or exit the program.

C Program for Simple Calculator

#include <stdio.h>

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

    do {
        // Display menu
        printf("Simple Calculator\n");
        printf("1. Addition\n");
        printf("2. Subtraction\n");
        printf("3. Multiplication\n");
        printf("4. Division\n");
        printf("Enter your choice (1-4): ");

        // User input
        scanf(" %c", &operator);

        // Check if the operator is valid
        if (operator < '1' || operator > '4') {
            printf("Invalid choice. Please enter a number between 1 and 4.\n");
            continue;
        }

        // Input two operands
        printf("Enter two operands separated by space: ");
        scanf("%lf %lf", &num1, &num2);

        // Perform operation based on user's choice
        switch (operator) {
            case '1':
                result = num1 + num2;
                printf("Result: %.2lf\n", result);
                break;
            case '2':
                result = num1 - num2;
                printf("Result: %.2lf\n", result);
                break;
            case '3':
                result = num1 * num2;
                printf("Result: %.2lf\n", result);
                break;
            case '4':
                // Check for division by zero
                if (num2 != 0) {
                    result = num1 / num2;
                    printf("Result: %.2lf\n", result);
                } else {
                    printf("Error: Division by zero.\n");
                }
                break;
        }

        // Ask the user if they want to perform another calculation
        printf("Do you want to perform another calculation? (y/n): ");
        scanf(" %c", &operator);

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

    printf("Calculator closed.\n");

    return 0;
}

Output Examples

Example 1: Addition

Simple Calculator
1. Addition
2. Subtraction
3. Multiplication
4. Division
Enter your choice (1-4): 1
Enter two operands separated by space: 5 3
Result: 8.00
Do you want to perform another calculation? (y/n): y

Example 2: Division by Zero

 
Simple Calculator
1. Addition
2. Subtraction
3. Multiplication
4. Division
Enter your choice (1-4): 4
Enter two operands separated by space: 10 0
Error: Division by zero.
Do you want to perform another calculation? (y/n): y

Example 3: Invalid Choice

 
Simple Calculator
1. Addition
2. Subtraction
3. Multiplication
4. Division
Enter your choice (1-4): 5
Invalid choice. Please enter a number between 1 and 4.

Example 4: Exit

 
Simple Calculator
1. Addition
2. Subtraction
3. Multiplication
4. Division
Enter your choice (1-4): 3
Enter two operands separated by space: 8 6
Result: 48.00
Do you want to perform another calculation? (y/n): n
Calculator closed.

Explanation

  1. Menu Display: The program presents the user with a menu of four operations: addition, subtraction, multiplication, and division.
  2. User Input: The user enters their choice (1-4) for the desired operation.
  3. Operand Input: Two operands are entered for the chosen operation.
  4. Switch…Case: The switch statement determines the operation based on the user’s choice and performs the corresponding arithmetic calculation.
  5. Division by Zero: Special consideration is given to division, and the program checks for division by zero to avoid runtime errors.
  6. Repeat or Exit: After displaying the result, the program asks the user if they want to perform another calculation. If the user chooses ‘y’ or ‘Y’, the loop continues; otherwise, the calculator is closed.

Conclusion

This C program serves as a simple calculator that can perform basic arithmetic operations. The switch statement is utilized to implement a menu-driven approach, making the program user-friendly and easy to navigate. Feel free to run the program with different inputs to explore its functionality. If you have any questions or need further clarification, please don’t hesitate to ask!

Leave a Reply

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