# Calculating Sum of all Elements in an Array using C++

## Calculating Sum of all Elements in an Array using C++:

In this article, I am going to discuss the program for Calculating the sum of all elements in an array using C++ Language with examples. Please read our previous articles, where we discussed Foreach Loop in C++ with Examples.

##### Calculating the sum of all elements in an array using C++:

Let us write the program for finding the sum of all the elements in an array. Let us take an array of size â€˜5â€™,

We have an array of size of â€˜7â€™ and indices start from 0 to 4. We want to add all these elements. So, this type of logic we have already seen by adding the numbers one by one i.e. sum of first n natural numbers. So, for that, we were taking the sum variable.

sum = 0;

So, we have taken â€˜sumâ€™, and initially, â€˜sumâ€™ is zero then, we have to add all the elements so that we can go through all those elements one by one. This will take us to all elements. So if we say A[i] then A[0] is â€˜6â€™, A[1] is â€˜3â€™, and so on. Now we want the sum.

##### Calculating the sum of all elements in an array using C++:

So, in that sum: 0 + 6 is â€˜6â€™.

Now that 6 + 3 is â€˜9â€™.

And that 9 + 7 is â€˜16â€™.

Now 16 + 9 is â€˜25â€™. And that 25 + 1 is â€˜26â€™.

So, in this way, we will calculate the sum of this array. So, this procedure you already know. Now let us write a program for it.

##### Program to Calculate the sum of elements of an array in C++:
```#include <iostream>
#include <conio.h>
using namespace std;

int main(){
int n, sum = 0;

cout << "Enter size of the array: ";
cin >> n;

int A[n];

cout << "Enter elements of the array:\n";
for(int i = 0; i < n; i++){
cin >> A[i];
sum += A[i];
}

cout << "Sum of the array: " << sum << endl;
getch();
}
```
###### Output:

So, this is a simple program to calculate the sum of all elements in an array using C++ Language.