Write a C program to enter marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer, calculate percentage and grade according to given conditions:
If percentage >= 90% : Grade A
If percentage >= 80% : Grade B
If percentage >= 70% : Grade C
If percentage >= 60% : Grade D
If percentage >= 40% : Grade E
If percentage < 40% : Grade F
Example
Input
Input marks of five subjects: 95 95 97 98 90
Output
Percentage = 95.00 Grade A
Required knowledge
Basic C programming, If else, Basic Mathematics
Logic to calculate percentage and grade
Did you remember you primary mathematics classes. In primary classes you have learnt about percentage. Just to give a quick recap I am putting below the formula to calculate percentage.
After you got the formula for calculating percentage. Let us now move on to the step by step descriptive logic to find percentage and grade.
- Read marks of five subjects in some variable say phy, chem, bio, math and comp.
- Calculate the percentage using above formula. Which is per = ((phy + chem + bio + math + comp) / 500) * 100. By applying little maths you can easily cut down this complex formula to simple one. Which is per = (phy + chem + bio + math + comp) / 5.
- On the basis of per find grade of the student.
- Check if per >= 90, then print "Grade A".
- Check all remaining conditions mentioned and grade the student.
Program to find percentage and grade
/** * C program to enter marks of five subjects and find percentage and grade */ #include <stdio.h> int main() { int phy, chem, bio, math, comp; //Five subjects float per; /* Reads marks of five subjects from user */ printf("Enter five subjects marks: "); scanf("%d%d%d%d%d", &phy, &chem, &bio, &math, &comp); /* Calculates percentage */ per = (phy + chem + bio + math + comp) / 5.0; printf("Percentage = %.2f\n", per); /* Finds grade according to the percentage */ if(per >= 90) { printf("Grade A"); } else if(per >= 80) { printf("Grade B"); } else if(per >= 70) { printf("Grade C"); } else if(per >= 60) { printf("Grade D"); } else if(per >= 40) { printf("Grade E"); } else { printf("Grade F"); } return 0; }
Note: %.2f is used to print fractional values up to two decimal places. You can also use %f normally to print fractional values up to six decimal places.
95
97
98
90
Percentage = 95.00
Grade A
Happy coding ;)
You may also like
- If else programming exercises index.
- C program to enter marks of five subjects and calculate total, average and percentage.
- C program to enter P, T, R and calculate Simple interest.
- C program to enter P, T, R and calculate Compound Interest.
- C program to enter any number and check whether it is even or odd.
- C program to check leap year.
- C program to count total number of notes in given amount.
- C program to enter cost price and selling price of a product and calculate total profit and loss.