Sum of Array Using Pointers

By | December 3, 2022

Sum of Array Using Pointers – Write a C program to input n numbers in one dimensional array and find sum of all elements with pointer arithmetic. You will love to read Understanding C Pointers Basic Concepts before reading this program.

C Program Sum of array using pointers

C Program Sum of Array Using Pointers

#include <stdio.h>
int main()
{
   int a[50], i,n, sum=0;
   int *ptr;
   printf("\n Write a C Program To:");
   printf("\n Input elements and Find Sum in");
   printf("\n Array Using Pointers in C:"); 
   printf("\n ------------------------------------\n");    
   
   printf(" Enter the number of elements of the array:");
   scanf("%d",&n);
   
   ptr = a;
   
   for(i=0;i<n;i++)
      {
	  printf(" Enter Element a[%d] : ",i);
	  scanf("%d",ptr++);
	  }
   
   ptr = a;
   for(i=0;i<n;i++)
      {
	  sum = sum + *ptr++;
	  }
	printf(" Sum of Array Elements using Pointer=%d",sum); 
	 
	return 0;
}

Output

Write a C Program To:
Input elements and Find Sum in
Array Using Pointers in C:
——————————————
Enter the number of elements of the array:5
Enter Element a[0] : 10
Enter Element a[1] : 20
Enter Element a[2] : 100
Enter Element a[3] : 800
Enter Element a[4] : 1
Sum of Array Elements using Pointer=931
——————————–

You may also like more on C Pointers:

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *