ANR Labs

A leap beyond infinity

Average and median of entered number list using C

The below code calculates and prints the average and median of the number list/string entered.

#include<stdio.h>

void main()
{
int i,j,n,flag;
double a[30],temp,sumx=0,avg;
printf("enter the value of n \n");
scanf("%d",&n);
printf("enter the numbers\n");
for(i=0;i<n;i++)
scanf("%lf",a[i]);
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=0;i<n;i++)
sumx+=a[i];
avg=sumx/n;
if(n%2==0)
flag=1;
else
flag=0;
if(flag)
printf("medians are %lf and %lf\n",a[(n/2)-1],a[n/2]);
else
printf("median is %lf\n",a[(n/2)]);
printf("average is %lf \n",avg);
getchar();
}
Average and median of entered number list using C

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to top