C program to enter student marks and calculate percentage and grade

Write a C program to enter marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer, calculate percentage and grade according to following conditions:
Percentage >= 90% : Grade A
Percentage >= 80% : Grade B
Percentage >= 70% : Grade C
Percentage >= 60% : Grade D
Percentage >= 40% : Grade E
Percentage < 40% : Grade F

How to calculate percentage and grade using if else in C programming.

Required knowledge

Basic C programming, If else

Program to calculate percentage and grade

/**
 * C program to enter marks of five subjects and calculate 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.


Output
Enter five subjects marks: 95
95
97
98
90
Percentage = 95.00
Grade A

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment