SET OF PROGRAMS

This blog provided "C" programs for beginners. Providing c programs for students.enjoy this blog.this blog include many c programs for engineering students, IT engineering students and programmers this blog is very useful.this blog post daily 1-5 programs for you.this programs use full for devlop games devlops application and much more. This blog programs useful for projects because all programs post with explanations.

Search Bar

Ads Here

Monday, 25 June 2018

C program for Find Average Of Class

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 .

#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);
}
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.000000
Explanation:
  1. 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
  2. printf("Enter number of students in class\n");
        scanf("%d",&num);
    Taking input from user 
  3. for(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
  4.  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
  5. Average of 2 students is 77 .This how the program is executed.

No comments:

Post a Comment