Average of Array With Pointer

By | December 3, 2022

Average of Array With Pointer – Write a C Program to calculate average of array elements using pointers.

C Program Find Average of Array with pointer

#include <stdio.h>
int main()
{
   int a[50], i,n, sum=0;
   float average;
   int *ptr;
   printf("\n Write a C Program To:");
   printf("\n Input elements and Find Average in");
   printf("\n Array Using Pointers in C:"); 
   printf("\n ------------------------------------\n");    
   
   printf(" Enter the number of elements of the array:");
   scanf("%d",&n);
   
   ptr = a;
   
   for(i=0;i<n;i++)
      {
	  printf(" Enter Element a[%d] : ",i);
	  scanf("%d",ptr++);
	  }
   
   ptr = a;
   for(i=0;i<n;i++)
      {
	  sum = sum + *ptr++;
	  }
	average = (float)sum / n;  
	printf(" Average of Array Elements using Pointer=%.2f",average); 
	 
	return 0;
}

Output

Write a C Program To:
Input elements and Find Average in
Array Using Pointers in C:
————————————
Enter the number of elements of the array:5
Enter Element a[0] : 10
Enter Element a[1] : 20
Enter Element a[2] : 30
Enter Element a[3] : 10
Enter Element a[4] : 5
Average of Array Elements using Pointer=15.00

Pointer Arithmetic for Average of array elements in C Program

You may also like more on C Pointers:

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *