programming-examples/c/_Basic/To compute the average of n given numbers using pointers.c

26 lines
575 B
C
Raw Normal View History

2019-11-15 12:59:38 +01:00
#include <stdio.h>
#include <conio.h>
void main()
{
int n,*p,sum=0,i;
float avg;
clrscr();
printf("\nHOW MANY NUMBERS: ");
scanf("%d",&n);
p=(int *) malloc(n*2);
if(p==NULL)
{
printf("\nMEMORY ALLOCATION UNSUCCCESSFUL");
exit();
}
for(i=0;i<n;i++)
{
printf("\nENTER NUMBER %d: ",i+1);
scanf("%d",(p+i));
}
for(i=0;i<n;i++)
sum=sum+*(p+i);
avg=(float)sum/n;
printf("\nTHE AVERAGE OF THE NUMBERS IS %0.2f",avg);
getch();
}