3 different C programs to print the grade of a student

C program to print the grade of a student:

In this post, we will learn how to print the grade of a student based on the marks obtained. The program will read the marks as input from the user and print the grade for that mark. We will use a predefined ranking system in the program to calculate the grade.

It will use the following grade system:

marks grade
>=90 A+
>=80 A
>=50 B
>=30 C
<30 F

Based on the value of the marks, the program will print its grade.

Example 1: C program to print the grade of a student with if-else statements:

Below is the complete C program to print the grade of a student with user-input marks:

#include <stdio.h>

int main()
{
	int marks;
	printf("Enter -1 to exit:\n");
	while (1)
	{
		printf("Enter the mark: ");
		scanf("%d", &marks);

		if (marks >= 90 && marks <= 100)
		{
			printf("Grade A+\n");
		}
		else if (marks >= 80 && marks <= 90)
		{
			printf("Grade A\n");
		}
		else if (marks >= 50 && marks <= 80)
		{
			printf("Grade B\n");
		}
		else if (marks >= 30 && marks <= 50)
		{
			printf("Grade C\n");
		}
		else if (marks >= 0 && marks <= 30)
		{
			printf("Grade F\n");
		}
		else
		{
			break;
		}
	}
	return 0;
}

In this program,

  • The while loop runs for an infinite amount of time. It will exit only if the user enters -1.
  • It asks the user to enter the value of marks and based on the marks, t prints the grade.
  • The if else-if else statements check for the value of the marks and based on its value, it prints the grade. It uses the above table to calculate the grade.
  • If the entered value is a negative number, it will break from the condition.

If you run this program, it will print the output as below:

Enter -1 to exit:
Enter the mark: 12
Grade F
Enter the mark: 55
Grade B
Enter the mark: 90
Grade A+
Enter the mark: 44
Grade C
Enter the mark: 77
Grade B
Enter the mark: -1

Example 2: C program to print the grade of a student with a separate function:

We can also use a separate function to write the same program. It will pass the marks to the function and the function will return the grade. Let’s write down the complete program:

#include <stdio.h>

void printGrade(int marks)
{
	if (marks >= 90 && marks <= 100)
	{
		printf("Grade A+\n");
	}
	else if (marks >= 80 && marks <= 90)
	{
		printf("Grade A\n");
	}
	else if (marks >= 50 && marks <= 80)
	{
		printf("Grade B\n");
	}
	else if (marks >= 30 && marks <= 50)
	{
		printf("Grade C\n");
	}
	else if (marks >= 0 && marks <= 30)
	{
		printf("Grade F\n");
	}
}
int main()
{
	int marks;
	printf("Enter -1 to exit:\n");
	while (1)
	{
		printf("Enter the mark: ");
		scanf("%d", &marks);

		if (marks >= 0)
		{
			printGrade(marks);
		}
		else
		{
			break;
		}
	}
	return 0;
}

We created a new function printGrade to print the grades. This function takes the marks as its parameter and it prints the grade. It uses the same if else-if else condition checks as the previous example.

If the value of the user input marks is greater than or equal to zero, it calls this method to print the grade. Else, it breaks from the infinite loop.

It will print similar results.

C print grade of student

Example 3: How to use macro to print the grade of a student:

We can use macros to write this program. It will make the program easy to change. For example, in the above program, if the mark is greater than or equal to 50, it is grade B. If we have to change it to 55, we will have to change this value in two places. If we use macros, it will be defined in one place and the program becomes more readable.

#include <stdio.h>
#define MAX 100
#define MIN 0
#define GRADE_A_PLUS_MIN 90
#define GRADE_A_MIN 80
#define GRADE_B_MIN 50
#define GRADE_C_MIN 30

void printGrade(int marks)
{
	if (marks >= GRADE_A_PLUS_MIN && marks <= MAX)
	{
		printf("Grade A+\n");
	}
	else if (marks >= GRADE_A_MIN && marks <= GRADE_A_PLUS_MIN)
	{
		printf("Grade A\n");
	}
	else if (marks >= GRADE_B_MIN && marks <= GRADE_A_MIN)
	{
		printf("Grade B\n");
	}
	else if (marks >= GRADE_C_MIN && marks <= GRADE_B_MIN)
	{
		printf("Grade C\n");
	}
	else if (marks >= MIN && marks <= GRADE_C_MIN)
	{
		printf("Grade F\n");
	}
}
int main()
{
	int marks;
	printf("Enter -1 to exit:\n");
	while (1)
	{
		printf("Enter the mark: ");
		scanf("%d", &marks);

		if (marks >= 0)
		{
			printGrade(marks);
		}
		else
		{
			break;
		}
	}
	return 0;
}

Here,

  • We defined a few macros
    • MAX is the maximum mark a student can get, i.e. 100 and MIN is the minimum mark i.e. 0.
    • GRADE_A_PLUS_MIN is the minimum mark to get Grade A+ i.e. 90.
    • GRADE_A_MIN is the minimum mark to get Grade A i.e. 80.
    • GRADE_B_MIN is the minimum mark to get Grade B i.e. 50.
    • GRADE_C_MIN is the minimum mark to get Grade A i.e. 30.

We replaced the macros in the if else-if else conditions. It will give similar output.

You might also like: