PPS Lab · Experiment 5
Class awarded for a percentage of marks
CSimple numeric problemsJNTUH R22 · CS108ES
Aim
To write a C program that reads a percentage of marks and prints the class awarded: below 40% is Failed, 40% to below 60% is Second class, 60% to below 70% is First class, and 70% or more is Distinction.
Algorithm
- Read the percentage.
- If it is less than 0 or more than 100, print a message and stop.
- If the percentage is less than 40, the result is Failed.
- Otherwise, if it is less than 60, the result is Second class.
- Otherwise, if it is less than 70, the result is First class.
- Otherwise the result is Distinction.
- Print the result.
Program
#include <stdio.h>
int main(void) {
double percentage;
printf("Enter percentage of marks: ");
if (scanf("%lf", &percentage) != 1) {
printf("Invalid input\n");
return 1;
}
if (percentage < 0 || percentage > 100) {
printf("Percentage must be between 0 and 100\n");
return 1;
}
if (percentage < 40) {
printf("Result: Failed\n");
} else if (percentage < 60) {
printf("Result: Second class\n");
} else if (percentage < 70) {
printf("Result: First class\n");
} else {
printf("Result: Distinction\n");
}
return 0;
}Output
Below 40%
Enter percentage of marks: 35
Result: FailedBetween 60% and 70%
Enter percentage of marks: 64.5
Result: First classExactly 70%
Enter percentage of marks: 70
Result: DistinctionRecorded by compiling and running this exact program. Typed input appears after each prompt.
How it works
The else if ladder checks the conditions from top to bottom and stops at the first true one. When the program reaches percentage < 60, it already knows the percentage is at least 40, so there is no need to write percentage >= 40 && percentage < 60.
The percentage is a double so that values such as 64.5 can be entered. 70 itself gives Distinction because the rule is 70% or more.
Common mistakes
Writing separate
ifstatements instead of anelse ifladder. A percentage of 35 is less than 40, 60 and 70, so it would print Failed, Second class and First class.Using
<=where the rule says below, for examplepercentage <= 60for Second class. Then exactly 60 gets the wrong class. Check each boundary value (40, 60, 70) against the rule.Declaring the percentage as
int, so an entry such as 64.5 is not read properly.Not checking for values outside 0 to 100, so an input such as 150 is shown as Distinction.
Viva questions
Try to answer each one aloud before you open it.
What is an else-if ladder?
It is a chain of
if ... else if ... elsestatements. The conditions are tested in order, and only the block for the first true condition runs.Why don't we need to write percentage >= 40 in the second condition?
The second condition is reached only if the first one,
percentage < 40, was false. So the percentage is already known to be 40 or more.Can this program be written with a switch statement?
Not directly, because
switchmatches exact integer values and not ranges. It can be done by switching on(int) percentage / 10, but theelse ifladder is clearer.What will the program print for exactly 60?
First class, because 60 is not less than 60 but is less than 70.