Writing a program that can efficiently calculate the percentages and assign grades based on predefine criteria is a fundamental task. In this article we will see how to develop a simple C program that accepts marks in five subjects, Calculates the overall percentage, and prints grades according to specified criteria.
Table of Contents
Problem Statement- WAP that accepts marks of five subjects and finds percentage and prints grades according to the following criteria:
- Between 90-100%—–Print ‘A’
- 80-90%—————–Print ‘B’
- 60-80%—————–Print ‘C
- ’Below 60%————-Print ‘D’
C Program to Calculate Percentage and Grades
#include <stdio.h>
void main()
{
int subject[5];
for (int i = 0; i < 5; i++)
{
printf("Enter the marks of subject %d ", i + 1);
scanf("%d", &subject[i]);
}
int totalmark = 0;
for (int i = 0; i < 5; i++)
{
totalmark += subject[i];
}
float percentage = totalmark / 5;
printf("percentage = %f\n", percentage);
if (percentage >= 90)
{
printf("Grade A");
}
else if (percentage >= 80 && percentage < 90)
{
printf("Grade B");
}
else if (percentage >= 60 && percentage < 80)
{
printf("Grade C");
}
else
{
printf("Grade D");
}
}
Output
Explanation of the Above C Program
- The above C program asks the user to enter the subject of each mark and stores it in an array
subject[5]
. - The total marks are calculated by summing the marks of all 5 subjects.
- Percentage is calculated by dividing the total marks by 5(total number of subjects).
- In the end by using the conditional statement if else the C program determines the grade based on the given criteria and prints the corresponding grade.
Happy Coding
2 thoughts on “WAP to Calculate Percentage and Grades in C”