Sum of Array using Pointer

Program Description

In this program the sum of array is calculated using pointers.

Program
/***********************************
*   Sum of array using pointer
*	
*	code.cheraus.com
*
***********************************/
#include<stdio.h>

int main()
{
  int arr[20],n,sum=0,*p,i;
  printf("\nEnter the no of element : ");
  scanf("%d",&n);
  
  printf("\nEnter the elements : ");
  
  for(i=0;i<n;i++)
   scanf("%d",&arr[i]);

  p = arr;
  
  for(i=0;i<n;i++)
  {
    sum = sum + *p;
    p++;
  }
  printf("\nSum of array is : %d\n",sum);
  return 0;
}

Output
Test Case 1:

Enter the no of element : 4

Enter the elements : 1
2
33
4


Sum of array is : 40

Comments
comments powered by Disqus