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
- Read two integers a and b.
- Read the operator op.
- 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.
- case
- Stop.
Program
#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
Enter two integers: 20 6
Enter an operator (+, -, *, /, %): *
20 * 6 = 120Modulo (remainder)
Enter two integers: 20 6
Enter an operator (+, -, *, /, %): %
20 % 6 = 2Division by zero
Enter two integers: 20 0
Enter an operator (+, -, *, /, %): /
Error: division by zero is not allowedRecorded 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
breakat 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. Acaselabel 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
caseruns, control continues into the following cases unless abreakstops it. This is called fall-through.breakjumps out of theswitch.What is the default case used for?
It runs when none of the
casevalues 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
charandenum. 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.
switchis easier to read in that case. It cannot test ranges likex > 10.