Find average of class
This program written by own me by my logic and this program completely run.
Dout for this program then contact me .
Output:
Enter number of students in class
10
Enter marks of all students
98
56
30
35
90
94
96
68
78
95
Average of class is 74.000000Explanation:
This program written by own me by my logic and this program completely run.
Dout for this program then contact me .
#include<stdio.h> main() { int i,j,sum=0,num; float avg; printf("Enter number of students in class\n"); scanf("%d",&num); int marks[num]; printf("Enter marks of all students\n"); for(i=0;i<num;i++) { scanf("%d",&marks[i]); sum+=marks[i]; } avg=(float)sum/num; printf("Average of class is %f\n",avg); }
Enter number of students in class
10
Enter marks of all students
98
56
30
35
90
94
96
68
78
95
Average of class is 74.000000Explanation:
- This program starts with initializing :
- sum → To store sum of marks
- i,j →Used as helping variable
- avg→ To store average of marks.
- num → To store number of students
printf("Enter number of students in class\n"); scanf("%d",&num);
Taking input from userfor(i=0;i<num;i++) { scanf("%d",&marks[i]); sum+=marks[i]; }
The loop will traverse from 0 to number of students in class.Each time you enter marks of a student that will be added to previous value of sum and stored in sum.For example: lets take num=2- When i=0,0<2 which is true so then it waits for us to enter number (say 98 is the number we entered).Then as sum=0 by default sum=sum+98=0+98=98.So, sum=98 .Then 'i' is incremented by 1.Now i=1.
- When i=1,1<2 which is true so then it waits for us to enter number (say 56 is the number we entered).Then as sum=98 from previous run sum=sum+56=98+56=98.So, sum=154.Now i will become 2
- As i=2 and 2<2 is falseso loop terminated and our final value of sum is 154
avg=(float)sum/num;
Finally As we took num=2 which means 2 students in class and got sum as 154 .So avg=154/2=77- Average of 2 students is 77 .This how the program is executed.
No comments:
Post a Comment