10. write a C program to find max., min. and avg. in an array of integers.


Program to find max., min. and avg. in an array of integers.

Source Code:

#include<stdio.h>
int main()
{

int a[100],max,min,i,n,sum;
float avg;
printf("enter the number of elements\n");
scanf(" %d",&n);
printf("enter %d elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
printf("a[%d]=%d\n",i,a[i]);
}
min=a[0];
max=a[0];
for(i=1;i<n;i++)
{
if(min>a[i])
min=a[i];
if(max<a[i])
max=a[i];
}
sum=0;
avg=0;
for(i=0;i<n;i++)
{
    sum=sum+a[i];
avg=(float)sum/n;
}
printf("minimum is %d\n",min);
printf("maximum is %d\n",max); 
printf("average is %f\n",avg); 
}

 Output:

 enter the number of elements
5
enter 5 elements
10
20
53
56
25
a[0]=10
a[1]=20
a[2]=53
a[3]=56
a[4]=25
minimum is 10
maximum is 56
average is 32.799999

No comments:

Post a Comment