Skip to lab content

UNIT 02 · Expression Evaluation

Integer calculator using switch

EXERCISE 02AC173 sample runs

THE SYLLABUS QUESTION

What you need to solve

Write a C program, which takes two integer operands and one operator from the user, performs the operation and then prints the result. Consider the operators +, -, *, /, % and use Switch Statement.
Input format & conventions

Enter operand, operator, operand; for example 20 / 3. Operands must be integers between -1,000,000 and 1,000,000.

UNDERSTAND THE IDEA

Explanation

A switch selects one arithmetic operation using the entered character. Each case ends with break to prevent fall-through.

Both operands are integers: / produces an integer quotient, truncating toward zero. The % operator gives the remainder. Multiplication is widened to long long before calculating the result.

PLAN BEFORE CODING

Algorithm

  1. Read the two operands and operator.
  2. Validate operand bounds.
  3. Select the matching switch case.
  4. Reject division or remainder by zero.
  5. Print the result or report an unsupported operator.

SEE THE CONTROL FLOW

Flowchart

Follow the arrows from Start. Diamonds ask a question; labeled arrows show the answer. A returning arrow repeats a loop. Function internals are grouped where needed; later input checks follow the rules in the program.

Flowchart for Integer calculator using switch: input, decisions, processing, output and loop paths

On a phone, scroll sideways to read the diagram at full size. Open full-size flowchart ↗

C17

Complete C program

Download .c
#include <stdio.h>

int main(void) {
    int a, b;
    char op;
    long long result;
    if (scanf("%d %c %d", &a, &op, &b) != 3 ||
        a < -1000000 || a > 1000000 || b < -1000000 || b > 1000000) {
        puts("Invalid input.");
        return 1;
    }
    switch (op) {
        case '+': result = (long long)a + b; break;
        case '-': result = (long long)a - b; break;
        case '*': result = (long long)a * b; break;
        case '/':
        case '%':
            if (b == 0) {
                puts("Cannot divide or take remainder by zero.");
                return 1;
            }
            result = op == '/' ? a / b : a % b;
            break;
        default:
            puts("Unsupported operator.");
            return 1;
    }
    printf("Result: %lld\n", result);
    return 0;
}
Open in compiler ↗

Code loads into the existing compiler. Enter the sample input there; sign-in and execution rules stay the same.

Compile and run locally
gcc -std=c17 switch-calculator.c -o lab
./lab

On Windows, run .\lab.exe after compiling with GCC. The interest program requires the math library where applicable.

FOLLOW THE VALUES

Dry run

Step / stateOperationResult
Input20 / 3Select case /
Integer division20 / 36
Related remainder20 % 32

CHECK THE BEHAVIOR

Sample input & output

Each output below was produced by compiling and running this exact program. Input values are entered in the stated order; the examples do not print input prompts.

Sample 1

INPUT
20 / 3
OUTPUT
Result: 6

Sample 2

INPUT
20 % 3
OUTPUT
Result: 2

Sample 3

INPUT
-7 * 4
OUTPUT
Result: -28

Common mistakes

  • Check the second operand before both / and %.
  • A missing break can run the next case accidentally.

WHY THIS GROWTH RATE?

Time and space complexity

Time O(1); auxiliary space O(1).

The switch selects one operator case and performs one arithmetic operation. It does not repeat operations based on the magnitude of either operand.

Reading the fixed operands, selecting a case, checking a zero divisor and computing the result take a bounded amount of work. Therefore time and auxiliary space are both O(1), under fixed-width integer arithmetic.

Big-O describes how work grows as the stated input quantity grows; fixed factors and lower-order terms are omitted. The analysis treats fixed-width arithmetic as constant cost and the published limits as practical safety bounds.