C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Decision Making Programs in C

Simple calculator using switch-case

Introduction

A calculator is one of the most common beginner programs in C. It helps you understand decision-making and control flow.

Unlike if-else, the switch-case structure allows us to handle multiple conditions more cleanly when we know the exact values to check (e.g., operators like +, -, *, /).

In this program:

  • The user enters two numbers and chooses an operator.
  • A switch statement checks the operator and performs the respective operation.
  • Division is handled carefully to avoid division by zero.

 

C Program: Simple calculator using switch-case

Method 1:

C

#include <stdio.h>

 

int main() {

    float num1, num2, result;

    char op;

 

    // Input numbers

    printf("Enter first number: ");

    scanf("%f", &num1);

 

    printf("Enter second number: ");

    scanf("%f", &num2);

 

    // Input operator

    printf("Enter operator (+, -, *, /): ");

    scanf(" %c", &op);  // Note the space before %c to ignore newline

 

    // Switch-case for calculation

    switch (op) {

        case '+':

            result = num1 + num2;

            printf("Result: %.2f + %.2f = %.2f\n", num1, num2, result);

            break;

 

        case '-':

            result = num1 - num2;

            printf("Result: %.2f - %.2f = %.2f\n", num1, num2, result);

            break;

 

        case '*':

            result = num1 * num2;

            printf("Result: %.2f * %.2f = %.2f\n", num1, num2, result);

            break;

 

        case '/':

            if (num2 != 0) {

                result = num1 / num2;

                printf("Result: %.2f / %.2f = %.2f\n", num1, num2, result);

            } else {

                printf("Error: Division by zero is not allowed.\n");

            }

            break;

 

        default:

            printf("Invalid operator!\n");

    }

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter first number: 12
Enter second number: 4
Enter operator (+, -, *, /): *

Result: 12.00 * 4.00 = 48.00

OUTPUT 2 :

Enter first number: 5
Enter second number: 2
Enter operator (+, -, *, /): ^

Invalid operator!


OUTPUT 3 :
Enter first number: 10
Enter second number: 0
Enter operator (+, -, *, /): /

Error: Division by zero is not allowed.

C Program: Simple calculator using switch-case

Method 2:

C

#include <stdio.h>

 

int main() {

    char operator;

    double num1, num2;

 

    // Prompt user for operator

    printf("Enter an operator (+, -, *, /): ");

    scanf("%c", &operator);

 

    // Prompt user for two numbers

    printf("Enter two numbers: ");

    scanf("%lf %lf", &num1, &num2);

 

    // Use switch-case to perform the operation

    switch (operator) {

        case '+':

            printf("%.2lf + %.2lf = %.2lf\n", num1, num2, num1 + num2);

            break; // Exit the switch statement after a case is matched

        case '-':

            printf("%.2lf - %.2lf = %.2lf\n", num1, num2, num1 - num2);

            break;

        case '*':

            printf("%.2lf * %.2lf = %.2lf\n", num1, num2, num1 * num2);

            break;

        case '/':

            if (num2 != 0) { // Handle division by zero

                printf("%.2lf / %.2lf = %.2lf\n", num1, num2, num1 / num2);

            } else {

                printf("Error: Division by zero is not allowed.\n");

            }

            break;

        default: // Executed if no case matches

            printf("Error: Invalid operator.\n");

    }

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter an operator (+, -, *, /): +
Enter two numbers: 5
10
5.00 + 10.00 = 15.00

OUTPUT 2 :

Enter an operator (+, -, *, /): -
Enter two numbers: 5 10
5.00 - 10.00 = -5.00


OUTPUT 3 :
Enter an operator (+, -, *, /): ^
Enter two numbers: 5
10
ERROR!
Error: Invalid operator.


OUTPUT 4 :

Enter an operator (+, -, *, /): /
Enter two numbers: 10 0
ERROR!
Error: Division by zero is not allowed.


Explanation:

  • #include <stdio.h>: This line includes the standard input/output library, providing functions like printffor printing to the console and scanf for reading user input.
  • int main() { ... }: This is the main function where the program execution begins.
  • Variable Declaration:
    • char operator;: Declares a character variable operatorto store the arithmetic operator entered by the user.
    • double num1, num2;: Declares two double-precision floating-point variables num1and num2 to store the numbers for calculation, allowing for decimal values.
  • User Input:
    • printf("Enter an operator (+, -, *, /): ");: Displays a message prompting the user to enter an operator.
    • scanf("%c", &operator);: Reads a single character from the user and stores it in the operator
    • printf("Enter two numbers: ");: Prompts the user to enter two numbers.
    • scanf("%lf %lf", &num1, &num2);: Reads two double-precision floating-point numbers from the user and stores them in num1and num2.
  • switch (operator) { ... }: This switchstatement evaluates the value of the operator
    • case '+':, case '-':, case '*':, case '/':: These caselabels represent the different possible values of operator. If the operator matches a case value, the code block associated with that case is executed.
      • Inside each arithmetic case, the corresponding operation is performed, and the result is printed using printf. %.2lfformats the output to two decimal places.
    • if (num2 != 0) { ... } else { ... }: For division, a check is included to prevent division by zero, which would cause a runtime error.
    • break;: The breakstatement is crucial. After a case is executed, break terminates the switch statement, preventing "fall-through" to subsequent case
    • default:: If the operatordoes not match any of the case labels, the code block under default is executed, indicating an invalid operator.
  • return 0;: Indicates successful program execution.