C Program to find the sum of array elements using pointers


In this program, You will learn how to find the sum of array elements using pointers in c.


sum = sum + *(ptr + i);

Example: How to find the sum of array elements using pointers in c.

#include<stdio.h>

int main() {

   int arr[5];
   int *ptr, i, sum = 0;

   printf("Enter five numbers:");
   for (i = 0; i < 5; i++) {
       scanf("%d", &arr[i]);
   }

   ptr = arr;
   for (i = 0; i < 5; i++) {
       sum = sum + *(ptr + i);
   }
   printf("Sum of all numbers:%d", sum);

   return 0;
}

Output:

Enter five numbers:10 20 30 40 50                                                                                                      
Sum of all numbers:150