C++ Sum of Array Program

Beginner
5 min read
Updated: Oct 2026
3 programs
Live preview

What Is the Sum of an Array?

The sum of an array is the result of adding every element once. This is the classic accumulator pattern: start at 0, then total += value for each item.

Remember
sum(arr) = arr[0] + arr[1] + ... + arr[n-1]

Example: [1, 2, 3, 4, 5]
  0 → 1 → 3 → 6 → 10 → 15

Empty array → 0
ArrayRunning totalSum
[1, 2, 3, 4, 5]1, 3, 6, 10, 1515
[10, -2, 7]10, 8, 1515
[](no steps)0

How to Find the Sum of an Array in C++

Four clear steps — one pass is enough.

  1. Set total = 0.
  2. Visit every element with a range-based for or an index loop.
  3. Add each value: total += value.
  4. Return total after the loop ends.
MethodIdeaTimeBest for
Range-based forAccumulator over each valueO(n)Interviews, clearest logic
Index for looptotal += arr[i]O(n)When you need the index
std::accumulateLibrary one-linerO(n)Production code

Pseudocode

Pseudocode
function SumArray(arr):
    total = 0
    for each value in arr:
        total = total + value
    return total

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Starttotal = 0
Steptotal += value
Empty arraySum is 0
NegativesAllowed; they reduce the total
OrderDoes not matter for ordinary integers
Classic check[1, 2, 3, 4, 5] → 15

Live Preview

Enter integers separated by commas or spaces to see the running total and final sum.

Whole numbers only. An empty input sums to 0.

Result
Array: [1, 2, 3, 4, 5]
Running: 0 → 1 → 3 → 6 → 10 → 15
Sum = 15

Worked Walkthrough — [1, 2, 3, 4, 5]

Watch the accumulator grow after each addition.

Calculation
total = 0

+ 1  → total = 1
+ 2  → total = 3
+ 3  → total = 6
+ 4  → total = 10
+ 5  → total = 15

Sum = 15

C++ Programs

Start with a fixed array and a simple helper. Then read values from input, and finish by tracing a running total with negatives.

Example 1 — Sum of a Fixed Array

Use a range-based for to add every element into an accumulator.

C++
#include <iostream>
using namespace std;

int sumArray(int arr[], int n)
{
    int total = 0;
    for (int i = 0; i < n; i++)
    {
        total += arr[i];
    }
    return total;
}

int main()
{
    int array[] = { 1, 2, 3, 4, 5 };
    int n = sizeof(array) / sizeof(array[0]);
    cout << "Sum of the array elements: " << sumArray(array, n) << endl;
    return 0;
}

How It Works

  1. total starts at 0.
  2. The loop visits 1, 2, 3, 4, then 5.
  3. Each visit does total += arr[i].
  4. The running values are 1, 3, 6, 10, then 15.
  5. Return the final total after the loop.

Example 2 — Sum with User Input

Read n, fill an array, then reuse the same summing helper.

C++
#include <iostream>
using namespace std;

int sumArray(int arr[], int n)
{
    int total = 0;
    for (int i = 0; i < n; i++)
    {
        total += arr[i];
    }
    return total;
}

int main()
{
    int n;
    cout << "Enter number of elements: ";
    cin >> n;

    if (n < 0)
    {
        cout << "n must be nonnegative." << endl;
        return 1;
    }

    int arr[100];
    if (n > 100)
    {
        cout << "This demo supports at most 100 elements." << endl;
        return 1;
    }

    for (int i = 0; i < n; i++)
    {
        cout << "Enter element " << (i + 1) << ": ";
        cin >> arr[i];
    }

    cout << "Sum of the array elements: " << sumArray(arr, n) << endl;
    return 0;
}

How It Works

  1. Read the length n and reject a negative size.
  2. Fill a fixed buffer (or a vector in larger programs).
  3. Call sumArray — input and summing stay separate.
  4. If n is 0, the array is empty and the sum is 0.
  5. Same helper as Example 1, so testing stays easy.

Example 3 — Running Total with Negatives

Print the accumulator after each step so negatives are easy to see.

C++
#include <iostream>
using namespace std;

int main()
{
    int arr[] = { 10, -2, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int total = 0;

    cout << "Running total:" << endl;
    for (int i = 0; i < n; i++)
    {
        total += arr[i];
        cout << "  after " << (arr[i] >= 0 ? "+" : "")
             << arr[i] << ": " << total << endl;
    }

    cout << "Final sum: " << total << endl;
    return 0;
}

How It Works

  1. Start at 0, then add 10 → 10.
  2. Add -2 → 8 (no special case for negatives).
  3. Add 7 → 15.
  4. A small + prefix keeps the added values easy to read.
  5. Tracing the running total is a strong whiteboard habit.

Edge Cases & Pitfalls

Check these before calling the solution done.

Empty

Sum is 0

An empty array never enters the loop, so the accumulator stays at 0.

Negatives

Just add them

Do not take absolute values unless the problem asks for that.

Overflow

Use a wider type if needed

Very large int arrays may need long long for the accumulator.

Size

Pass length with C arrays

C-style arrays decay to pointers — pass n, or prefer vector<int>.

Time and Space Complexity

ProgramTimeExtra space
Index / range loopO(n)O(1)
User-input build + sumO(n)O(n) for the array
Running-total traceO(n)O(1)

You must look at every element at least once, so O(n) is optimal for the general case.

Key Takeaways

  • Pattern: accumulator starts at 0, then adds each element once.
  • Loop: for (int i = 0; i < n; i++) total += arr[i];
  • Empty: no iterations → sum stays 0.
  • Test set: [1,2,3,4,5] → 15; [10,-2,7] → 15; [] → 0.

One line: start at zero, add every element once, return the total.

Frequently Asked Questions

Start with total = 0, then loop through the array and add each element with total += value.
Zero is the additive identity. Starting there makes the final total equal the sum of all elements, including the empty-array case.
Yes. The same loop works; negatives reduce the running total.
Prefer a loop in interviews to show the accumulator. In production code, accumulate is fine.
O(n) time and O(1) extra space for a single pass over n elements.

Next: Sum of Digits

Add the digits of a number with n % 10.

Sum of digits tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful