c program to find the average and range of n numbers

Average and Range



Average of n numbers is the sum of numbers divided by n. The range of n numbers is the difference between the largest and smallest element.

Explanation of the program



The program reads the limit and stores it in the variable n. The value of each element is stored in the variable a, each time the value stored in the variable l and s is checked with the value of a to find the new smallest and largest element. The variable sum stores the sum of these numbers.

Program



#include<stdio.h>

#include<conio.h>

void main()

{

int n,a,l=0,flag=1,sum=0,range,i=1,s,avg;

clrscr();

printf("Enter the limit:  ");

scanf("%d",&n);

printf("Enter the numbers: ");

while(i<=n)

{

scanf("%d",&a);

if(flag==1)

{

s=a;

flag=0;

}

if(a>l)

{

l=a;

}

if(a<s)

{

s=a;

}

sum=sum+a;

i++;

}

avg=sum/n;

range=l-s;

printf("Average= %d",avg);

printf("\nRange= %d",range);

getch();

}


Output



c program for average and range
C program to find the average and range

No comments: