Skip to content
Laviyx Learn

Try

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

  1. Read the percentage.
  2. If it is less than 0 or more than 100, print a message and stop.
  3. If the percentage is less than 40, the result is Failed.
  4. Otherwise, if it is less than 60, the result is Second class.
  5. Otherwise, if it is less than 70, the result is First class.
  6. Otherwise the result is Distinction.
  7. Print the result.

Program

C
#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%

Output 1
Enter percentage of marks: 35
Result: Failed

Between 60% and 70%

Output 2
Enter percentage of marks: 64.5
Result: First class

Exactly 70%

Output 3
Enter percentage of marks: 70
Result: Distinction

Recorded 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 if statements instead of an else if ladder. 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 example percentage <= 60 for 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 ... else statements. 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 switch matches exact integer values and not ranges. It can be done by switching on (int) percentage / 10, but the else if ladder is clearer.

  • What will the program print for exactly 60?

    First class, because 60 is not less than 60 but is less than 70.

Learn the concepts

All Programming for Problem Solving Lab experiments →