C Program to Find Sum of Array Elements
To find the sum of array elements in C, use a loop to add each element to a sum variable like
for(int i = 0; i < n; i++) sum += arr[i];.Examples
Inputarr = {1, 2, 3, 4, 5}, n = 5
OutputSum of array elements: 15
Inputarr = {10, 20, 30}, n = 3
OutputSum of array elements: 60
Inputarr = {}, n = 0
OutputSum of array elements: 0
How to Think About It
To find the sum, think of adding each number in the list one by one. Start with zero, then add the first number, then the second, and so on until all numbers are added. The final total is the sum.
Algorithm
1
Get the number of elements in the array.2
Initialize a variable sum to zero.3
Loop through each element in the array.4
Add the current element to sum.5
After the loop ends, return or print the sum.Code
c
#include <stdio.h> int main() { int arr[] = {1, 2, 3, 4, 5}; int n = 5; int sum = 0; for (int i = 0; i < n; i++) { sum += arr[i]; } printf("Sum of array elements: %d\n", sum); return 0; }
Output
Sum of array elements: 15
Dry Run
Let's trace the array {1, 2, 3, 4, 5} through the code
1
Initialize sum
sum = 0
2
Add first element
sum = 0 + 1 = 1
3
Add second element
sum = 1 + 2 = 3
4
Add third element
sum = 3 + 3 = 6
5
Add fourth element
sum = 6 + 4 = 10
6
Add fifth element
sum = 10 + 5 = 15
| Iteration | Current Element | Sum After Addition |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 2 | 3 |
| 3 | 3 | 6 |
| 4 | 4 | 10 |
| 5 | 5 | 15 |
Why This Works
Step 1: Initialize sum to zero
We start with sum = 0 because we have not added any elements yet.
Step 2: Loop through each element
The for loop goes through each element in the array one by one.
Step 3: Add each element to sum
Inside the loop, we add the current element to sum using sum += arr[i].
Step 4: Print the final sum
After the loop ends, sum holds the total, which we print using printf.
Alternative Approaches
Using while loop
c
#include <stdio.h> int main() { int arr[] = {1, 2, 3, 4, 5}; int n = 5, i = 0, sum = 0; while (i < n) { sum += arr[i]; i++; } printf("Sum of array elements: %d\n", sum); return 0; }
This uses a while loop instead of for loop; functionally the same but slightly different syntax.
Using recursion
c
#include <stdio.h> int sumArray(int arr[], int n) { if (n <= 0) return 0; return arr[n-1] + sumArray(arr, n-1); } int main() { int arr[] = {1, 2, 3, 4, 5}; int n = 5; printf("Sum of array elements: %d\n", sumArray(arr, n)); return 0; }
This uses recursion to add elements; elegant but uses more memory due to function calls.
Complexity: O(n) time, O(1) space
Time Complexity
The program loops through all n elements once, so time grows linearly with input size.
Space Complexity
Only a few variables are used regardless of input size, so space is constant.
Which Approach is Fastest?
The iterative for loop is fastest and simplest; recursion adds overhead and uses more memory.
| Approach | Time | Space | Best For |
|---|---|---|---|
| For loop | O(n) | O(1) | Simple and efficient for all array sizes |
| While loop | O(n) | O(1) | Same as for loop, just different syntax |
| Recursion | O(n) | O(n) | Good for learning recursion but less efficient |
Always initialize your sum variable to zero before adding array elements.
Forgetting to initialize sum to zero can cause incorrect results due to garbage values.