Grade of a Student: flowchart, algorithm and program

This program reads marks out of 100 and prints a grade: A, B, C, D or F. It uses an if-else ladder that checks the highest cut-off first and stops at the first condition that is true.

▶ Watch the dry run step by step →

Algorithm

  1. Step 1: Start.
  2. Step 2: Read marks.
  3. Step 3: If marks >= 90, go to step 4; otherwise go to step 5.
  4. Step 4: Print Grade: A. Go to step 12.
  5. Step 5: If marks >= 75, go to step 6; otherwise go to step 7.
  6. Step 6: Print Grade: B. Go to step 12.
  7. Step 7: If marks >= 60, go to step 8; otherwise go to step 9.
  8. Step 8: Print Grade: C. Go to step 12.
  9. Step 9: If marks >= 40, go to step 10; otherwise go to step 11.
  10. Step 10: Print Grade: D. Go to step 12.
  11. Step 11: Print Grade: F.
  12. Step 12: Stop.

Pseudocode

BEGIN
    READ marks
    IF marks >= 90 THEN
        PRINT Grade: A
    ELSE
        IF marks >= 75 THEN
            PRINT Grade: B
        ELSE
            IF marks >= 60 THEN
                PRINT Grade: C
            ELSE
                IF marks >= 40 THEN
                    PRINT Grade: D
                ELSE
                    PRINT Grade: F
                END IF
            END IF
        END IF
    END IF
END

Program in C, Python and Java

Grade of a Student program in C

#include <stdio.h>

int main() {
    int marks;
    printf("Enter marks: ");
    scanf("%d", &marks);

    if (marks >= 90)
        printf("Grade: A\n");
    else if (marks >= 75)
        printf("Grade: B\n");
    else if (marks >= 60)
        printf("Grade: C\n");
    else if (marks >= 40)
        printf("Grade: D\n");
    else
        printf("Grade: F\n");

    return 0;
}

Grade of a Student program in Python

marks = int(input("Enter marks: "))

if marks >= 90:
    print("Grade: A")
elif marks >= 75:
    print("Grade: B")
elif marks >= 60:
    print("Grade: C")
elif marks >= 40:
    print("Grade: D")
else:
    print("Grade: F")

Grade of a Student program in Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter marks: ");
        int marks = sc.nextInt();

        if (marks >= 90)
            System.out.println("Grade: A");
        else if (marks >= 75)
            System.out.println("Grade: B");
        else if (marks >= 60)
            System.out.println("Grade: C");
        else if (marks >= 40)
            System.out.println("Grade: D");
        else
            System.out.println("Grade: F");
    }
}

Sample input: marks = 82
Output: Enter marks: Grade: B

How it works

  • The program reads one integer into `marks` using `scanf`.
  • It first checks `marks >= 90`. If true, it prints Grade A and skips the rest of the ladder.
  • If that fails, it checks `marks >= 75`, then `marks >= 60`, then `marks >= 40`, in that order.
  • Because the checks go from high to low, reaching a condition means all the higher ones were already false. So `marks >= 75` really means 75 to 89.
  • If none of the conditions match, the final `else` prints Grade F.
  • For marks = 82: 82 >= 90 is false, 82 >= 75 is true, so it prints Grade: B and ends.

Dry run (trace table)

StepLineBoxmarksOutput
13Start
26Read marks82Enter marks:
38marks >= 90?82
410marks >= 75?82
511Print Grade: B82Grade: B
620Stop82

Time and space complexity

Time: O(1) · Space: O(1). At most four comparisons are made, regardless of the input value.

Common mistakes

  • Writing the conditions in the wrong order, for example checking `marks >= 40` first. Then every mark of 40 or more gets D, because that condition is true first.
  • Using `>` instead of `>=`, so a boundary mark like exactly 90 or 75 gets the lower grade.
  • Using separate `if` statements instead of `else if`. A mark like 95 would then match several conditions and print more than one grade.
  • Not handling marks below 0 or above 100. The program as written would give A for 150 and F for -5.

Viva questions

Q1. Why do we write &marks in scanf and not just marks?

scanf needs the address of the variable so it can store the number you type directly into marks. Without & it would receive the value of marks (garbage) instead of its location.

Q2. Explain how the if-else-if ladder works in this program. What happens when marks is 80?

Conditions are checked from top to bottom and the first true one runs; the rest are skipped. For 80, marks >= 90 is false, marks >= 75 is true, so it prints Grade: B and skips the others.

Q3. Why don't we need to write marks < 90 in the second condition (marks >= 75)?

We only reach the else-if if the earlier condition failed, so marks is already below 90. The ladder order itself gives the upper bound, so only the lower bound is checked.

Q4. What grade is printed for marks = 90, 75 and 40, and why?

90 gives A, 75 gives B and 40 gives D, because the conditions use >= so the boundary value belongs to the higher grade. If we used > instead, these would drop to the lower grade.

Q5. What does the program print for a negative number like -5, or for 150? Is that correct?

-5 prints Grade: F because it fails every condition and reaches the else. 150 prints Grade: A because it is >= 90. Both are logically wrong, since the program has no check that marks lies between 0 and 100.

Q6. What happens if the order of conditions is reversed, starting with marks >= 40 first?

Every mark of 40 or more would print Grade: D, since the first true condition wins. The ladder only works if conditions go from the highest threshold down to the lowest.

Q7. What is the time complexity of this program?

O(1), constant time. There are no loops, and at most four comparisons are made whatever the input value is.

Q8. What would you change to accept decimal marks like 74.5, or to add input validation?

Declare marks as float and use %f in scanf, so 74.5 is compared properly. For validation, add an initial check such as if (marks < 0 || marks > 100) that prints 'Invalid marks' before the grade ladder.

Frequently asked questions

Why do we check the highest grade first in the if-else ladder?

Each `else if` only runs when the earlier conditions were false. Checking from high to low means each test needs just one comparison, like `marks >= 75`, with no upper limit.

What grade does the program give for exactly 90, 75, 60 or 40?

Because it uses `>=`, exactly 90 gives A, 75 gives B, 60 gives C and 40 gives D. Only marks below 40 get F.

Can I use a switch statement instead of if-else for grades?

Not directly, because a switch compares exact values, not ranges. You could use `marks / 10` as the switch value, but the if-else ladder is simpler and clearer for cut-offs like 75.

More programs

← All programs

Try AI Flowchart free →