Beginner Array Problems C

Sum of Array Elements in C with Output

Read n integers into an array in C and print the sum of all elements.

  • TimeO(n)
  • SpaceO(1) extra, besides the array itself
  • Hints3
  • Read3 min
Quick answer

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.

Arraysfor loopscanf inputAccumulator variable
1

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.

Input

An integer n, then n integers.

Output

Sum = <total>

Examples

Example 1
Inputn = 5, arr = 12 5 8 20 15
OutputSum = 60

12 + 5 + 8 + 20 + 15 = 60.

Example 2
Inputn = 3, arr = -4 10 -6
OutputSum = 0

Negative 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
2

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?
3

Hints

Open one at a time — try again after each.
1 Hint 1
Start with int sum = 0, not an uninitialised sum.
2 Hint 2
Loop with for (i = 0; i < n; i++) and do sum += arr[i].
3 Hint 3
Print the sum after the loop ends, not inside it.
4

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

  1. Read n and the n values into arr.
  2. Set sum = 0.
  3. For each index i from 0 to n - 1, add arr[i] to sum.
  4. 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.

5

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.

6

Solution

Reveal the full C solution
solution.c
#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
  • scanf needs &arr[i], the address of the element.
  • (float) sum / n converts before dividing. Without the cast, 155 / 5 is integer division and any decimal part would be lost.
  • arr[100] means n must 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.

TimeO(n)
SpaceO(1) extra, besides the array itself
7

Review 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

Building your programming basics?

CodeBegun’s programs build problem-solving with mentor review and mock interviews, online or in Madhapur, Hyderabad.

Chat with us