C Program to Compute sum of the array elements using pointers
Posted by Samath
Last Updated: January 02, 2017

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

Code:

#include<stdio.h>
#include<conio.h>
int main() {
   int nums[10];
   int i, sum = 0;
   int *ptr;
 
   printf("\nEnter 10 elements : ");
 
   for (i = 0; i < 10; i++)
      scanf("%d", &nums[i]);
 
   ptr = nums; 
 
   for (i = 0; i < 10; i++) {
      sum = sum + *ptr;
      ptr++;
   }
 
   printf("Sum: %d", sum);
   
   return 0;
}

The program uses the pointer to traverse the array and adds up the element values there.