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.
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
- Read the two operands and operator.
- Validate operand bounds.
- Select the matching switch case.
- Reject division or remainder by zero.
- 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.
On a phone, scroll sideways to read the diagram at full size. Open full-size flowchart ↗
#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;
}
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
./labOn Windows, run .\lab.exe after compiling with GCC. The interest program requires the math library where applicable.
FOLLOW THE VALUES
Dry run
| Step / state | Operation | Result |
|---|---|---|
| Input | 20 / 3 | Select case / |
| Integer division | 20 / 3 | 6 |
| Related remainder | 20 % 3 | 2 |
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
20 / 3
Result: 6
Sample 2
20 % 3
Result: 2
Sample 3
-7 * 4
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.