How to Create a C Program to Display Grades Based on Marks (If-Else Ladder)

Introduction:

In this blog post, we’ll demonstrate how to write a C program to display grades based on marks using an if-else ladder. This program is a great way to practice conditional statements in C programming, which are essential for handling user input and making decisions based on that input.

Problem:

The task is to write a program that reads the marks of a student and displays the corresponding grade based on these ranges:

  • Distinction: Marks between 80 and 100
  • First Class: Marks between 60 and 79
  • Second Class: Marks between 40 and 59
  • Fail: Marks below 40

Let’s create a simple C program to display grades according to these ranges using the if-else ladder.


Solution:

Here’s the C program to convert marks to grades based on the given conditions:

#include <stdio.h>

int main() {
int marks;

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

// Check grade using if-else ladder
if (marks >= 80 && marks <= 100) {
printf("Grade: Distinction\n");
}
else if (marks >= 60 && marks < 80) {
printf("Grade: First Class\n");
}
else if (marks >= 40 && marks < 60) {
printf("Grade: Second Class\n");
}
else if (marks < 40) {
printf("Grade: Fail\n");
}
else {
printf("Invalid marks entered.\n");
}

return 0;
}

Understanding the Code:

  1. Input Handling: We use scanf to take marks from the user. The value entered is stored in the variable marks.
  2. If-Else Ladder Logic:
    • The program uses the if-else ladder to check different ranges of marks:
      • Marks between 80 and 100 result in a “Distinction”.
      • Marks between 60 and 79 result in “First Class”.
      • Marks between 40 and 59 are categorized as “Second Class”.
      • Any value below 40 results in a “Fail”.
  3. Edge Case Handling: The program also includes an else block to handle invalid or unexpected inputs (like negative marks or non-integer values).

Why Use the If-Else Ladder in C?

  • Conditional Checks: The if-else ladder is an essential tool in C programming. It allows you to check multiple conditions in sequence, making decisions based on various inputs.
  • Logical Flow: It provides a straightforward way to categorize marks and map them to grades. This logic can be extended for more complex applications, such as adding more grade categories or incorporating different types of user input.

Conclusion:

This C program to display grades is an excellent example of how to use if-else statements in C to classify data. By understanding this concept, you can easily extend the logic to create more complex programs in C. Whether you’re a beginner or a more advanced programmer, mastering conditionals is crucial for effective programming.

Feel free to modify this program by adding new features, like handling invalid marks or calculating average grades.

Previous Article

C Program to Check Character Type (Capital, Small, Digit, or Special Character)

Next Article

C Program to Prepare Pay Slip with DA, HRA, MA, PF and Net Salary Calculation

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨