Skip to content
Laviyx Learn

Try

PPS Lab · Experiment 9

Simple calculator using a switch statement

CExpression evaluationJNTUH R22 · CS108ES

Aim

To write a C program that reads two integers and an operator (+, -, *, / or %), performs the operation using a switch statement and prints the result, reporting an error for division or modulo by zero.

Algorithm

  1. Read two integers a and b.
  2. Read the operator op.
  3. Use switch (op):
    • case +: print a + b.
    • case -: print a − b.
    • case *: print a × b.
    • case /: if b is 0, print an error; otherwise print a / b.
    • case %: if b is 0, print an error; otherwise print a % b.
    • default: print that the operator is not valid.
  4. Stop.

Program

C
#include <stdio.h>

int main(void) {
    int a, b;
    char op;

    printf("Enter two integers: ");
    if (scanf("%d %d", &a, &b) != 2) {
        printf("Invalid input\n");
        return 1;
    }
    printf("Enter an operator (+, -, *, /, %%): ");
    if (scanf(" %c", &op) != 1) {
        printf("Invalid input\n");
        return 1;
    }

    switch (op) {
    case '+':
        printf("%d + %d = %d\n", a, b, a + b);
        break;
    case '-':
        printf("%d - %d = %d\n", a, b, a - b);
        break;
    case '*':
        printf("%d * %d = %d\n", a, b, a * b);
        break;
    case '/':
        if (b == 0) {
            printf("Error: division by zero is not allowed\n");
            return 1;
        }
        printf("%d / %d = %d\n", a, b, a / b);
        break;
    case '%':
        if (b == 0) {
            printf("Error: modulo by zero is not allowed\n");
            return 1;
        }
        printf("%d %% %d = %d\n", a, b, a % b);
        break;
    default:
        printf("Error: '%c' is not a valid operator\n", op);
        return 1;
    }
    return 0;
}

Output

Multiplication

Output 1
Enter two integers: 20 6
Enter an operator (+, -, *, /, %): *
20 * 6 = 120

Modulo (remainder)

Output 2
Enter two integers: 20 6
Enter an operator (+, -, *, /, %): %
20 % 6 = 2

Division by zero

Output 3
Enter two integers: 20 0
Enter an operator (+, -, *, /, %): /
Error: division by zero is not allowed

Recorded by compiling and running this exact program. Typed input appears after each prompt.

How it works

The operands are integers, so / gives the whole-number part of the quotient: 20 / 6 is 3, and 20 % 6 is the remainder 2.

Each case ends with break. Without it, the program would fall through and also run the statements of the cases below.

The space in scanf(" %c", &op) skips the newline left after reading the two numbers, so the operator is read correctly.

Common mistakes

  • Leaving out break at the end of a case. Execution falls through into the next case, so choosing + would also print the subtraction, multiplication and so on.

  • Writing case "+": with double quotes. A case label must be a character constant '+', not a string.

  • Reading the operator with scanf("%c", &op) without the space. It picks up the newline left after the numbers, and the program says the operator is invalid.

  • Not checking for b = 0 before / and %. Integer division by zero crashes the program.

Viva questions

Try to answer each one aloud before you open it.

  • Why is break needed in a switch statement?

    After a matching case runs, control continues into the following cases unless a break stops it. This is called fall-through. break jumps out of the switch.

  • What is the default case used for?

    It runs when none of the case values match, here when the user types an operator that is not +, -, *, / or %. It is optional, but it is good practice to include it.

  • Which data types can be used in a switch expression?

    Only integer types, which include char and enum. Floating-point values and strings cannot be used.

  • What is the result of 20 / 6 and 20 % 6 with integers?

    20 / 6 is 3, because integer division drops the fraction, and 20 % 6 is 2, the remainder.

  • When is a switch statement better than an if-else ladder?

    When one variable is compared with several fixed values, as with the operator here. switch is easier to read in that case. It cannot test ranges like x > 10.

Learn the concepts

All Programming for Problem Solving Lab experiments →