C Program to find the sum of all array elements – In this article, we will detail in on all the ways to find the sum of all array elements in C programming.
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.
The means used in this distinct piece are as follows:
- Using Standard Method
- Using Function
- Using Recursion
An array in the C Programming language can be termed as a sequence of orderly arrangements of things of a single type.
This is what an array looks like:
As you can see, 5 elements are present in this array. Each element has been given the location a[0] to a[4].
Thus, adding up all the elements would give us:
Sum = 5 + 2 + 7 + 9 + 6 = 29.
Thus, the multitude of methods used to calculate the sum of all the elements of an array is as follows:
Using Standard Method
- Read the array size and store it in the variable n.
2) Scanf function reads the entered element and store the element in to the array as a[i] using for(i=0;i<n;i++) loop.
3) sum=0, add each element of the array to the sum value using for loop with the structure for(i=0; i<n;i++) as sum=sum+a[i].
4) After all iterations of for loop, we will get the sum of all array elements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <stdio.h> #include <conio.h> int main() { int a[1000],i,n,sum=0; printf("Enter size of the array : "); scanf("%d",&n); printf("Enter elements in array : "); for(i=0; i<n; i++) { scanf("%d",&a[i]); } for(i=0; i<n; i++) { sum+=a[i]; } printf("sum of array is : %d",sum); return 0; } |
Output:
1 2 3 4 5 6 7 |
Enter size of the array: 5 Enter elements in array: 7 8 9 4 5 sum of an array is:33 |
Using Function
- The function sumofarray() is the user defined function which calculates the sum of all array elements of an array.
2) The main() function calls the sumofarray() function by passing an array, size of an array.
3)The function sumofarray(int a[], int n) adds the each element of the array to the sum value using for loop with the structure for(i=0;i<n;i++) and returns the sum value to main() function.
4) The main() function prints the sum value which contains the sum of all elements of the given array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <stdio.h> #include <conio.h> int sumofarray(int a[],int n) { int i,sum=0; for(i=0; i<n; i++) { sum+=a[i]; } return sum; } int main() { int a[1000],i,n,sum; printf("Enter size of the array : "); scanf("%d", &n); printf("Enter elements in array : "); for(i=0; i<n; i++) { scanf("%d",&a[i]); } sum=sumofarray(a,n); printf("sum of array is :%d",sum); } |
Output:
1 2 3 4 5 6 7 |
Enter size of the array: 5 Enter elements in array: 1 2 4 3 1 sum of array is:11 |
Using Recursion
- The main function calls the sumofarray() function by passing an array, size of the array, i value.
2) The function sumofarray() calculates the sum of all elements of the array by calling itself until the condition i<n becomes false.
3) The sumofarray() returns the sum value to main() function,then main() function prints the sum value.
Here sumofarray() function is the recursive function which calls itself until the condition becomes false.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <stdio.h> #include <conio.h> int sumofarray(int a[],int n,int i) { if(i<n) return a[i]+sumofarray(a,n,++i); return 0; } int main() { int a[1000],i,n,sum; printf("Enter size of the array : "); scanf("%d", &n); printf("Enter elements in array : "); for(i=0; i<n; i++) { scanf("%d", &a[i]); } sum=sumofarray(a,n,0); printf("sum of array is :%d",sum); } |
1 2 3 4 5 |
Enter size of the array : 3 Enter elements in array: 5 5 5 the sum of an array is:15 |