C Program to Enter Marks of Five Subjects and Calculate Percentage and Grade
In this article, you will learn how to find the Total, Average, Percentage, and Grade values of the marks of five subjects in the c language.
Example
Enter the marks of five subjects::
95
85
74
64
53
The Total marks are: 371.00 / 500.00
The Average marks are: 74.20
The Percentage is: 74.20%
The Grade is: 'C'
You should have the knowledge of the following topics in c programming to understand this program:
- C Functions
- C main() function
- C printf() function
Standard Formula
Total = marks1 + marks2 + marks3 + marks4 + marks5
Average = Total / 5.0
Percentage = (Total / 500.0) x 100
Where marks1
, marks2
, marks3
, marks4
, and marks5
are the marks of five subjects.
In this article, we solve this problem in five methods:
- Using the If-else ladder
- Using the Switch Case
- Using the function
- Using the nested If-else ladder
- Using the conditional operator
C Program to Find Grade of a Student Using If else Ladder
// C Program to Find Grade of a Student Using If else Ladder
#include <stdio.h>
int main() {
float subject_1, subject_2, subject_3, subject_4, subject_5;
float total, average, percentage;
char grade;
printf("Enter the marks of five subjects::\n");
scanf("%f%f%f%f%f", &subject_1, &subject_2, &subject_3, &subject_4, &subject_5);
// It will calculate the Total, Average and Percentage
total = subject_1 + subject_2 + subject_3 + subject_4 + subject_5;
average = total / 5.0;
percentage = (total / 500.0) * 100;
// It will calculate the Grade
if (average >= 90)
grade = 'A';
else if (average >= 80 && average < 90)
grade = 'B';
else if (average >= 70 && average < 80)
grade = 'C';
else if (average >= 60 && average < 70)
grade = 'D';
else
grade = 'E';
// It will produce the final output
printf("\nThe Total marks is: \t%.2f / 500.00\n", total);
printf("\nThe Average marks is:\t%.2f\n", average);
printf("\nThe Percentage is: \t%.2f%%\n", percentage);
printf("\nThe Grade is: \t'%c'\n", grade);
return 0;
}
Output
Enter the marks of five subjects::
95
85
74
64
53
The Total marks is: 371.00 / 500.00
The Average marks is: 74.20
The Percentage is: 74.20%
The Grade is: 'C'
Explanation
Average: we take the marks of five subjects as input after that the sum of these marks is divided by 5 then It will return the average value of the marks.
Percentage: we made a division of the sum of the user's marks by 500 and then multiplied by 100 then It will return the percentage value of the marks.
Grade: we compared the value of the Average marks with this range of marks to find the Grade of students like:
If Average marks >= 90
then Its Grade is 'A'
If Average marks >= 80 and < 90
then Its Grade is 'B'
If Average marks >= 70 and < 80
then Its Grade is 'C'
If Average marks >= 60 and < 70
then Its Grade is 'D'
If Average marks < 60
then Its Grade is 'E'
C Program to Find Grade of a Student Using Switch Case
// C Program to Find Grade of a Student Using Switch Case
#include <stdio.h>
int main() {
float subject_1, subject_2, subject_3, subject_4, subject_5;
float total, average, percentage;
char grade;
int sAvg;
printf("Enter the marks of five subjects::\n");
scanf("%f%f%f%f%f", &subject_1, &subject_2, &subject_3, &subject_4, &subject_5);
// It will calculate the Total, Average and Percentage
total = subject_1 + subject_2 + subject_3 + subject_4 + subject_5;
average = total / 5.0;
percentage = (total / 500.0) * 100;
// It will divides average by 10 check switch expression
sAvg = (int) average / 10;
// It will calculate the Grade
switch (sAvg) {
case 10:
grade = 'A';
break;
case 9:
grade = 'A';
break;
case 8:
grade = 'B';
break;
case 7:
grade = 'C';
break;
case 6:
grade = 'D';
break;
default:
grade = 'E';
break;
}
// It will produce the final output
printf("\nThe Total marks is: \t%.2f / 500.00\n", total);
printf("\nThe Average marks is:\t%.2f\n", average);
printf("\nThe Percentage is: \t%.2f%%\n", percentage);
printf("\nThe Grade is: \t'%c'\n", grade);
return 0;
}
Output
Enter the marks of five subjects::
98
88
76
66
65
The Total marks is: 393.00 / 500.00
The Average marks is: 78.60
The Percentage is: 78.60%
The Grade is: 'C'
You should also read these things
C++ Program to Calculate Percentage and Grade
Java Program to Calculate Grade of students
Python Program to Calculate Total Marks Percentage and Grade of a Student
C Program to Find Grade of a Student Using Function
// C Program to Find Grade of a Student Using Function
#include <stdio.h>
// @Custom function to Display Percentage & Grade
void CalcPercentGrade(float subject_1, float subject_2, float subject_3, float subject_4, float subject_5) {
float total, average, percentage;
char grade;
// It will calculate the Total, Average and Percentage
total = subject_1 + subject_2 + subject_3 + subject_4 + subject_5;
average = total / 5.0;
percentage = (total / 500.0) * 100;
// It will calculate the Grade
if (average >= 90)
grade = 'A';
else if (average >= 80 && average < 90)
grade = 'B';
else if (average >= 70 && average < 80)
grade = 'C';
else if (average >= 60 && average < 70)
grade = 'D';
else
grade = 'E';
// It will display final calculation
printf("\nThe Total marks is: \t%.2f / 500.00\n", total);
printf("\nThe Average marks is:\t%.2f\n", average);
printf("\nThe Percentage is: \t%.2f%%\n", percentage);
printf("\nThe Grade is: \t'%c'\n", grade);
}
// @Driver function to Run this Program
int main() {
float subject_1, subject_2, subject_3, subject_4, subject_5;
printf("Enter the marks of five subjects::\n");
scanf("%f%f%f%f%f", &subject_1, &subject_2, &subject_3, &subject_4, &subject_5);
// It will produce the final output
CalcPercentGrade(subject_1, subject_2, subject_3, subject_4, subject_5);
return 0;
}
C Program to Find Grade of a Student Using Nested If else
// C Program to Find Grade of a Student Using Nested If else
#include <stdio.h>
int main() {
float subject_1, subject_2, subject_3, subject_4, subject_5;
float total, average, percentage;
char grade;
printf("Enter the marks of five subjects::\n");
scanf("%f%f%f%f%f", &subject_1, &subject_2, &subject_3, &subject_4, &subject_5);
// It will calculate the Total, Average and Percentage
total = subject_1 + subject_2 + subject_3 + subject_4 + subject_5;
average = total / 5.0;
percentage = (total / 500.0) * 100;
// It will calculate the Grade
if (average >= 90) {
grade = 'A';
}
else {
if (average >= 80 && average < 90) {
grade = 'B';
}
else {
if (average >= 70 && average < 80) {
grade = 'C';
}
else {
if (average >= 60 && average < 70) {
grade = 'D';
}
else {
grade = 'E';
}
}
}
}
// It will produce the final output
printf("\nThe Total marks is: \t%.2f / 500.00\n", total);
printf("\nThe Average marks is:\t%.2f\n", average);
printf("\nThe Percentage is: \t%.2f%%\n", percentage);
printf("\nThe Grade is: \t'%c'\n", grade);
return 0;
}
Also, visit these links
C Program to Convert Celsius to Fahrenheit
Sum of Two Numbers in C Program
C Program to Find Grade of a Student Using Conditional Operator
// C Program to Find Grade of a Student Using Conditional Operator
#include <stdio.h>
int main() {
float subject_1, subject_2, subject_3, subject_4, subject_5;
float total, average, percentage;
char grade;
printf("Enter the marks of five subjects::\n");
scanf("%f%f%f%f%f", &subject_1, &subject_2, &subject_3, &subject_4, &subject_5);
// It will calculate the Total, Average and Percentage
total = subject_1 + subject_2 + subject_3 + subject_4 + subject_5;
average = total / 5.0;
percentage = (total / 500.0) * 100;
// It will calculate the Grade
grade = average >= 90 ? 'A' : (average >= 80 && average < 90 ? 'B' : (average >= 70 && average < 80 ? 'C' : (average >= 60 && average < 70 ? 'D' : 'E')));
// It will produce the final output
printf("\nThe Total marks is: \t%.2f / 500.00\n", total);
printf("\nThe Average marks is:\t%.2f\n", average);
printf("\nThe Percentage is: \t%.2f%%\n", percentage);
printf("\nThe Grade is: \t'%c'\n", grade);
return 0;
}