In this C Program, we accept a mark of a student and find the percentage and grade according to the given mark. First of all, we need to find the percentage of the subject mark using maximum mark of the subject.
Conditions:
80 %+ = A Grade
60 %+ = B Grade
40 %+ = C Grade
Below 40 % => Failed, No grade.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
int mark,maxmark;
float percentage;
printf("Enter the subject mark :");
scanf("%d",&mark);
printf("Enter the maximum mark of the subject :");
scanf("%d",&maxmark);
percentage = mark*100.0 / maxmark;
if(percentage >= 80)
printf("\n Percentage is %.2f and Grade : A", percentage);
else if (percentage >= 60)
printf("\n Percentage is %.2f and Grade B", percentage);
else if (percentage >= 40)
printf("\n Percentage is %.2f and Grade : C", percentage);
else
printf("\n Percentage is %.2f, so Failed", percentage);
getch();
}
The percentage is calculated by dividing the given mark with maximum mark and multiplies with 100. Due to the implicit conversion of the C compiler (mark/maxmark)100 will not work, and then the answer will be zero.
Note that, the program code will change according to given conditions.
Program with Fractional Percentage:
If you want fractional part on your percentage, you must declare the variable ‘percentage’ as float.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
int mark,maxmark;
float percentage;
printf("Enter the subject mark :");
scanf("%d",&mark);
printf("Enter the maximum mark of the subject :");
scanf("%d",&maxmark);
percentage = mark*100.0 / maxmark;
if(percentage >= 80)
printf("\n Percentage is %.2f and Grade : A", percentage);
else if (percentage >= 60)
printf("\n Percentage is %.2f and Grade B", percentage);
else if (percentage >= 40)
printf("\n Percentage is %.2f and Grade : C", percentage);
else
printf("\n Percentage is %.2f, so Failed", percentage);
getch();
}
To get percentage as rational numbers, then you need to multiply mark with 100.0. If you multiply with 100 only, then due to the variable type of mark and maxmark are integer, answer will be converted into integer. So the fraction part of the percentage will be removed.
For printing the percentage value, we can round the fractional part with number of digit. If we need two precious digit of the fraction, so we type ‘.2%f’ instead of %f.
Sample Output:

Be First to Comment