Sum of Array Elements in C with Output
Read n integers into an array in C and print the sum of all elements.
- Time
O(n) - Space
O(1) extra, besides the array itself - Hints3
- Read3 min
To find the sum of array elements in C, declare int sum = 0, then loop i from 0 to n - 1 and do sum += arr[i]. After the loop, sum holds the total. For {12, 5, 8, 20, 15} the sum is 60. It takes O(n) time and O(1) extra space. Use long long for the sum if the total can go past 2147483647.
Problem statement
Write a C program to find the sum of array elements. Read n, then read n
integers into an array, add them all, and print the sum.
For the array {12, 5, 8, 20, 15} the output is Sum = 60.
An integer n, then n integers.
Sum = <total>
Examples
n = 5, arr = 12 5 8 20 15Sum = 6012 + 5 + 8 + 20 + 15 = 60.
n = 3, arr = -4 10 -6Sum = 0Negative values are added the same way. -4 + 10 - 6 = 0.
Constraints
- 1 <= n <= 100 for the fixed-size array used here
- The total must fit in the type used for sum
Think before you code
Reveal the questions to ask yourself first
- What should the sum be before you have added anything?
- Which loop index gives you each element of the array, from first to last?
- What happens if the total is larger than an int can hold?
Hints
Open one at a time — try again after each.1 Hint 1
2 Hint 2
3 Hint 3
Approach
Reveal the step-by-step approach
C array sum program: the idea
Keep a running total, called an accumulator. It starts at 0 and grows by one element each time round the loop.
| i | arr[i] | sum after this step |
|---|---|---|
| start | - | 0 |
| 0 | 12 | 12 |
| 1 | 5 | 17 |
| 2 | 8 | 25 |
| 3 | 20 | 45 |
| 4 | 15 | 60 |
Steps
- Read
nand thenvalues intoarr. - Set
sum = 0. - For each index
ifrom 0 ton - 1, addarr[i]tosum. - Print
sum.
If an array is new to you, read what is an array
first. Indexes start at 0, so the last valid index is n - 1.
Dry run
Walk through the example step by step
arr = {12, 5, 8, 20, 15}, n = 5:
i | i < 5? | arr[i] | sum = sum + arr[i]
--+--------+--------+-------------------
0 | yes | 12 | 0 + 12 = 12
1 | yes | 5 | 12 + 5 = 17
2 | yes | 8 | 17 + 8 = 25
3 | yes | 20 | 25 + 20 = 45
4 | yes | 15 | 45 + 15 = 60
5 | no | - | loop ends
The program prints Sum = 60.
Solution
Reveal the full C solution
#include <stdio.h>
int main(void) {
int arr[] = {12, 5, 8, 20, 15};
int n = sizeof(arr) / sizeof(arr[0]);
int sum = 0;
for (int i = 0; i < n; i++) {
sum = sum + arr[i];
}
printf("Array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\nSum = %d\n", sum);
return 0;
}Output:
Array: 12 5 8 20 15
Sum = 60
sizeof(arr) / sizeof(arr[0]) gives the number of elements, 5 here, so you do not
have to count them by hand. This trick only works where the array was declared, not
inside a function that receives it as a parameter.
Array input and sum with scanf
Most lab questions ask you to read the values from the user. This version also prints the average.
#include <stdio.h>
int main(void) {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[100];
printf("Enter %d numbers: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
printf("Sum = %d\n", sum);
printf("Average = %.2f\n", (float) sum / n);
return 0;
}
Enter number of elements: 5
Enter 5 numbers: 10 20 30 40 55
Sum = 155
Average = 31.00
scanfneeds&arr[i], the address of the element.(float) sum / nconverts before dividing. Without the cast,155 / 5is integer division and any decimal part would be lost.arr[100]meansnmust not be more than 100.
Sum of array elements in C using a function
#include <stdio.h>
long long array_sum(int arr[], int n) {
long long sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
return sum;
}
int main(void) {
int marks[] = {78, 85, 69, 91, 88};
int big[] = {2000000000, 2000000000};
int mixed[] = {-4, 10, -6};
printf("Total marks = %lld\n", array_sum(marks, 5));
printf("Big sum = %lld\n", array_sum(big, 2));
printf("Mixed sum = %lld\n", array_sum(mixed, 3));
return 0;
}
Total marks = 411
Big sum = 4000000000
Mixed sum = 0
The function must receive n, because inside it arr is only a pointer and
sizeof no longer gives the array size. The sum is long long, so two values of
2000000000 add up correctly. With an int sum, 4000000000 would overflow.
The Java version is on find the sum of all elements in an array. For the average on its own, see find the average of the elements in an array.
O(n)O(1) extra, besides the array itselfReview before the interview
Common mistakes
- Declaring int sum; without = 0. Local variables in C start with garbage values.
- Looping with i <= n, which reads arr[n], one past the last element.
- Writing scanf("%d", arr[i]) without &. The program may crash.
- Printing the sum inside the loop, so a running total is printed n times.
- Dividing sum / n for the average without a cast, which drops the decimal part.
Edge cases to test
- n = 1: the sum is the single element.
- All negative values: the sum is negative, and the same code works.
- Very large values: use long long for the sum and print with %lld.
- n larger than the declared array size: the program writes outside the array. Check n before reading.
Interview follow-ups
- How would you find the sum of only the even elements?
- How would you find the sum using a while loop or recursion?
- How would you find the sum of each row of a 2D array?
Keep practising
Find the Sum of All Elements in an Array
Hints · Dry run · SolutionSolve Similar problemFind the Average of the Elements in an Array
Hints · Dry run · SolutionSolve Similar problemLargest Element in an Array in C
Hints · Dry run · SolutionSolve Similar problemCount the Even and Odd Numbers in an Array
Hints · Dry run · SolutionSolve Related learningWhat Is An Array
TutorialLearn Related learningLoops In Programming
TutorialLearn Related learningLearn C Programming Step By Step
TutorialLearn CareerPilot AIPractice under pressure
Take an AI mock interview and get question-by-question feedback.
Mock interviewStartBuilding your programming basics?
CodeBegun’s programs build problem-solving with mentor review and mock interviews, online or in Madhapur, Hyderabad.
