 # C Programming Code Examples

## C > If Else and Switch Case Code Examples

### C program to enter student marks and find percentage and grade

``` C program to enter student marks and find percentage and grade Write a C program to input 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 Logic to calculate percentage and grade In primary mathematics classes you have learned about percentage. Just to give a quick recap, below is the formula to calculate percentage. Percentage formula Input marks of five subjects in some variable say phy, chem, bio, math and comp. Calculate percentage using formula per = (phy + chem + bio + math + comp) / 5.0;. Carefully notice I have divided sum with 5.0, instead of 5 to avoid integer division. On the basis of per find grade of the student. Check if(per >= 90) then, print "Grade A". If per is not more than 90, then check remaining conditions mentioned and print grade. #include <stdio.h> int main() { int phy, chem, bio, math, comp; float per; /* Input marks of five subjects from user */ printf("Enter five subjects marks: "); scanf("%d%d%d%d%d", &phy, &chem, &bio, &math, &comp); /* Calculate percentage */ per = (phy + chem + bio + math + comp) / 5.0; printf("Percentage = %.2f\n", per); /* Find 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; } ``` 