C Program to Compute sum of the array elements using pointers

Write a ‘C’ Program to compute the sum of all elements stored in an array using pointers 

/*C Program to Compute sum of the array elements using pointers*/ 
#include<stdio.h>

main() {
   int Array[10],j,sum = 0,*ptr;;

   printf("Enter 5 elements to Array  :\n");

   for (j = 0; j < 5; j++)
      scanf("%d", &Array[j]);   /*Accept the 5 elements from the user in the array*/

   ptr = Array;             /* a=&a[0]  store address of the array into the pointer */

   for (j = 0; j < 5; j++)          
   {
      sum = sum + *ptr;   
/* De-referencing pointer we are able to get the value at address*/     
       ptr++;
   }

   printf("sum of array elements is : %d", sum);
}

Related Posts