Basic

Switch Case

Goto

Operators

if Statement

Nested if

While Loop

For Loop

Array

2D Array

Patterns

Excersises


C Program to Calculate Student Grade Based on Marks


The following example shows, how to calculate grade based on marks.

  • Takes user input for marks (0–100) and stores it in a variable.
  • Validates if the input marks are within the valid range (0 to 100).
  • Uses nested if-else conditions to determine the grade based on mark ranges.
  • Displays the grade (A to F) or an error message if the input is invalid.

Example : pgm.c

#include <stdio.h>

int main() {
  int marks;
  
  // Input marks from user
  printf("Enter your marks: ");
  scanf("%d", &marks);

  // Checking conditions using nested if
  if (marks >= 0 && marks <= 100) { 
      if (marks >= 90) {
          printf("Grade: A\n");
      } else if (marks >= 80) {
          printf("Grade: B\n");
      } else if (marks >= 70) {
          printf("Grade: C\n");
      } else if (marks >= 60) {
          printf("Grade: D\n");
      } else if (marks >= 50) {
          printf("Grade: E\n");
      } else {
          printf("Grade: F (Fail)\n");
      }
  } else {
      printf("Invalid marks! Please enter marks between 0 and 100.\n");
  }

    return 0;
}

Output :

Enter your marks: 80
Grade: B