Today, let’s take a look at how to calculate aggregate and percentage in C. Here, we will accept marks of five subjects from the user and display average and percentage. This is the third program of [I] section of Chapter 1:Getting Started of the book, “Let us C” by “Yashwant Kanetkar”. If you are unsure about any part and more information, put it down in the comments section below or look it up on Google if you want a quick response. We will try to get back to you as soon as we see the comment. Share it with your friends as it might be helpful to them as well.
Question:
If the marks obtained by a student in five different subjects are input through the keyboard, write a program to find out the aggregate marks and percentage marks obtained by the student. Assume that the maximum marks that can be obtained by a student in each subject is 100.
Solution to calculate aggregate and percentage in C
Here, we have accepted marks of five subjects using the scanf() function in C. Then, we have stored the total of marks in the ‘total’ variable. Since, average and percentage can be decimal values, we have declared floating variables, ‘average’ and ‘percentage’ respectively. In both these variables we have calculated the average and percentage using their formulas respectively. Lastly, we display the output using printf() function.
Download the source code:
Download ProgramProgram to calculate aggregate and percentage in C
#include<stdio.h> #include<conio.h> void main(){ int m1,m2,m3,m4,m5,total; float average, percentage; printf("Enter marks for subject one - "); scanf("%d",&m1); printf("Enter marks for subject two - "); scanf("%d",&m2); printf("Enter marks for subject three - "); scanf("%d",&m3); printf("Enter marks for subject four - "); scanf("%d",&m4); printf("Enter marks for subject five - "); scanf("%d",&m5); total=m1+m2+m3+m4+m5; average=total/5; percentage=(total*100)/500; printf("\nThe average of five subjects is %f",average); printf("\nPercentage=%f%%",percentage); getch(); }
Output:
Enter marks for subject one – 87
Enter marks for subject two – 87
Enter marks for subject three – 97
Enter marks for subject four – 94
Enter marks for subject five – 78The average of five subjects is 88.000000
Percentage=88.000000%
The above is correct but why is it shown differently in Let us C solutions. In Let us C solutions, percentage is shown as aggr/5. I thought Let us C solutions was a reliable book.