C program to calculate total average and percentage of five subjects

Write a C program to input marks of five subjects of a student from user and calculate total, average and percentage of all subjects. How to calculate total, average and percentage of marks of five subjects in C programming.

Example:
Input marks of five subjects: 95 76 85 90 89
Output total = 435
Average = 87
Percentage = 87.00

Required knowledge

Fundamentals of C, Data types, Talking user input in C

Program

/**
 * C program to calculate total, average and percentage of five subjects
 */

#include <stdio.h>

int main()
{
    float n1, n2, n3, n4, n5; //n1,n2...n5 will store marks of five subjects.
    float total, average, percentage;

    // Reads marks of all five subjects
    printf("Enter marks of five subjects: \n");
    scanf("%f%f%f%f%f", &n1, &n2, &n3, &n4, &n5);

    // Calculates total, average and percentage one by one
    total = n1 + n2 + n3 + n4 + n5;
    average = total / 5.0;
    percentage = (total/500.0) * 100;

    printf("Total marks = %.2f\n", total);
    printf("Average marks = %.2f\n", average);
    printf("Percentage = %.2f", percentage);

    return 0;
} 


Output
X
_
Enter marks of five subjects:
95
76
85
90
89
Total marks = 435.00
Average marks = 87.00
Percentage = 87.00

Note: %.2f is used for printing fractional value only up to two decimal places. You can also use %f for printing fractional values normally up to six decimal places.

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).

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. I made the excercice using do while loop so it calculates marks for 20 students

      Delete
    2. Well that's, a good approach Errahli. You can also use for loop or while loop for easiness.

      Delete