Average of array elements C program

It’s an average of array elements C program that inputs numbers from the user, and stores these numbers in an array, then find the average of all array elements using the loop, and print result on the screen.

To understand this program, it’s better you should know the following topics:

Program (1): to find the average of array elements.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],i,n;
float av,sum=0;
printf("Enter the number of elements(upto 100) to be entered in the list : ",n);
scanf("%d",&n);
//accepting values into array
for(i=0;i<n;i++)
{
printf("Enter %d element : ",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
sum=sum+a[i];
printf("sum=%f",sum);
av=sum/n;
printf("\naverage=%f",av);
getch();
}

Output (1)

Enter the number of elements(upto 100) to be entered in the list : 3
Enter 1 element : 12
Enter 2 element : 23
Enter 3 element : 1
sum=36.000000
average=12.000000

Published by

Electrical Workbook

We provide tutoring in Electrical Engineering.

Leave a Reply

Your email address will not be published. Required fields are marked *