# Write a ‘C’ Program to compute the sum of all elements stored in an array using pointers?

941 views
Write a ‘C’ Program to compute the sum of all elements stored in an array using pointers?
posted Dec 3, 2014

``````#include<stdio.h>
#include<conio.h>
void main() {
int numArray[10];
int i, sum = 0;
int *ptr;

printf("\nEnter 10 elements : ");

for (i = 0; i < 10; i++)
scanf("%d", &numArray[i]);

ptr = numArray; /* a=&a[0] */

for (i = 0; i < 10; i++) {
sum = sum + *ptr;
ptr++;
}

printf("The sum of array elements : %d", sum);
}
``````

Output :

``````Enter 10 elements : 11 12 13 14 15 16 17 18 19 20
The sum of array elements is 155
``````

We can use recursive method, something like

``````int N = array_size;
int *ptr = array;
int sum = 0;

int sum(int *ptr )
{
return *ptr

N--;
return sum + sum(ptr + N));
}
``````

Use the method sum(ptr+N)

Similar Questions
+1 vote

Write a program to print the sum of the elements of the array with the given below condition. If the array has 6 and 7 in succeeding orders, ignore 6 and 7 and the numbers between them for the calculation of sum.
Eg1) Array Elements - 10,3,6,1,2,7,9
O/P: 22
[i.e 10+3+9]
Eg2) Array Elements - 7,1,2,3,6
O/P:19
Eg3) Array Elements - 1,6,4,7,9
O/P:10

input:
one
two
three

output:
three
two
one

+1 vote

Write a program to remove the duplicate elements in an array and print
Eg) Array Elements - 12, 34, 12, 45, 67, 89
O/P: 12,34,45,67,89