C++ How To Calculate the Sum of Array Elements
Calculate the Sum of Array Elements
Loop through the array, and add each element to a sum:
Example
int numbers[] = {10, 20, 30, 40, 50};
int sum = 0;
for (int num : numbers) {
sum += num;
}
cout << "Sum: " << sum;
Explanation:sum starts at 0. The loop adds each number to it.
10 + 20 + 30 + 40 + 50 is 150.