Wednesday 7 September 2016

Sum and Average of array elements using pointer

Write a C program to read 10 integers and store them in an array using pointers. Print their sum and average


#include<stdio.h>
#include<math.h>
void main()
{
                int a[10],sum=0;
                float avg=0;
                int i;
               
                printf("\nEnter the n array elements:");
                for(i=0;i<10;i++)
                                scanf("%d",(a+i));

                for(i=0;i<10;i++)
                                sum=sum+*(a+i);
   
                avg = (float)sum/10;

                printf("\nSum is = %d", sum);
                printf("\nMean is = %f", avg);
}

Output:
Enter the n array elements:1
2
3
4
5
6
7
8
9
10

Sum is = 55

Mean is = 5.500000

No comments:

Post a Comment