- Write a C program to read marks of N subjects and find Total, Average and Percentage marks.
- Wap in C to find average and percentage marks of all subjects.
Required Knowledge
- C printf and scanf functions
- For loop in C
- C Arithmetic Operators
We will first read the number of subjects and then marks of all subjects using for loop and scanf function. To get the total marks, we add the marks of all subject and to calculate average marks and percentage we will use below mentioned formulae.
- Average Marks = Marks_Obtained/Number_Of_Subjects
- Percentage of Marks = (Marks_Obtained/Total_Marks) X 100
C program to find total, average and percentage marks of subjects
/** * C program to calculate total, average and percentage of all subjects */ #include <stdio.h> int main(){ int subjects, i; float marks, total=0.0f, averageMarks, percentage; /* * Take number of subjects as imput from user */ printf("Enter number of subjects\n"); scanf("%d", &subjects); /* * Take marks of subjects as input */ printf("Enter marks of subjects\n"); for(i = 0; i < subjects; i++){ scanf("%f", &marks); total += marks; } averageMarks = total / subjects; /* Each subject is of 100 Marks*/ percentage = (total/(subjects * 100)) * 100; printf("Total Marks of %d Subjects = %0.4f\n",subjects,total); printf("Average Marks = %.4f\n", averageMarks); printf("Percentage = %.4f", percentage); return 0; }
Output
Enter number of subjects 4 Enter marks of subjects 50 60 70 80 Total Marks of 4 Subjects = 260.0000 Average Marks = 65.0000 Percentage = 65.0000