Array Elements summation in C++


Array elements summation is a famous task for programmers, as We can take the array elements from the user also, Similarly we can compute sum of element of array also. Here is an example which is able of compute sum of the elements of an array:

// Array Elements summation in C++
#include <iostream>
using namespace std;
int main()

const int arraySize = 10; // constant variable indicating size of array

int a[ arraySize ] = { 87, 68, 94, 100, 83, 78, 85, 91, 76, 87 };

int total = 0;

// sum contents of array a

for ( int i = 0; i < arraySize; ++i )
total += a[ i ];
cout << "Sum of array elements: " << total << endl;

In the above example we created and initialized an array and now we are going to compute sum of elements of this array. We defined a variable “total” and then we applied a for loop in order to get access to every element of the array, then we added every element into the “total” and at the end we have the computed sum.  Now after the loop when  we compute sum and displayed it on the display.