Write a program to find sum of all numbers in an array using pointer.

 C File =>


/* Write a program to find sum of all numbers in an array using pointer.*/


#include<stdio.h>
int main() {
int num[30],n,i,sum;
//declaring pointer to array
int (*p)[30]=&num;
printf("\n Enter how many elements :");
scanf("%d",&n);
printf("\n Accepting data :\n");
for(i=0;i<n;i++)
{
   printf("\n Enter number%d:",i+1);
   scanf("%d",&num[i]);
}
//calculating sum using pointer to array
for(i=0;i<n;i++)
sum=sum+(*p)[i];
printf("\n Sum of array elements = %d",sum);
return 0;
}

Comments

Popular posts from this blog

Write a program to allocate memory dynamically for n integers. Accept the elements and calculate their sum and average

C program to accept names of n cities and search for city named “Pune”.