Given a user defined array the task is to calculate the sum of the elements of the array using pointer.
Example:
Consider an array of size 5.
| 5 | 1 | 7 | 3 | 6 |
The sum of all elements will be 5+1+7+3+6 = 22
C
#include <stdio.h>
int main()
{
int n=0,sum=0;
printf("Enter the size of array: ");
scanf("%d",&n);
int ar[n];
int *p=ar;
printf("Enter the array elements:\n");
for(int i=0;i<n;i++)
{
scanf("%d",p);
p++;
}
p=ar;
for(int i=0;i<n;i++)
{
sum=sum+*p;
p++;
}
printf ("Sum of all the elements: %d",sum);
return 0;
}C