C Program to find the sum of all elements stored in array using pointers

We can find the sum of elements stored in an array using pointers. For example: if the number of elements to be added are 4 and if we give the elements one by one as 4 5 6 3, then the sum of elements stored in the array will be 18. The below given C program will find the sum of all elements in array using pointers. Kindly check out the program to find the sum of all elements in array using pointers

Sum of elements in array using pointers Program

#include<stdio.h>

int main(){

int n, i, sum = 0, elements[10];

int *pointr;

printf("How many elements to add : "); //How many numbers to be added

scanf("%d", &n);

printf("

Give the elements one by one : ");

for (i = 0; i < n; i++)

scanf("%d", &elements[i]); // Assigning Starting Address to Pointer Variable

pointr = elements; for (i = 0; i < n; i++) {

sum = sum + *pointr;

++pointr;

}

printf("



The sum of array elements : %d", sum);

} Sample Output: How many elements to add : 4

Give the elements one by one : 4 5 6 3

The sum of array elements : 18

Related Popular Posts: