Programming Example
C program to calculate total average and percentage of five subjects
Study this program carefully to understand the logic, output, and explanation in a structured way.
/**
* C program to calculate total, average and percentage of five subjects
*/
#include"stdio.h"
int main()
{
float n1, n2, n3, n4, n5;
float total, average, percentage;
// Read marks of all five subjects
printf("Enter marks of five subjects: \n");
scanf("%f%f%f%f%f", &n1, &n2, &n3, &n4, &n5);
// Calculate total, average and percentage one by one
total = n1 + n2 + n3 + n4 + n5;
average = total / 5.0;
percentage = (total / 500.0) * 100;
// Print the result
printf("Total marks = %.2f\n", total);
printf("Average marks = %.2f\n", average);
printf("Percentage = %.2f \n", percentage);
return 0;
}
Enter marks of five subjects:
1 2 3 4 5
Total marks = 15.00
Average marks = 3.00
Percentage = 3.00Press any key to continue . . .
First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.