An array is a collection of elements stored at contiguous memory locations. It’s the most used data structure in programming. In this article, you’ll learn how to find the sum of all elements in an array using C++, Python, and JavaScript.
Problem Statement
You’re given an array of numbers, and you need to calculate and print the sum of all elements in the given array.
Example 1: Let arr = [1, 2, 3, 4, 5]
Therefore, the sum of all elements of the array = 1 + 2 + 3 + 4 + 5 = 15.
Thus, the output is 15.
Example 2: Let arr = [34, 56, 10, -2, 5, 99]
Therefore, the sum of all elements of the array = 34 + 56 + 10 + (-2) + 5 + 99 = 202.
Thus, the output is 202.
Approach to Find the Sum of All Elements in an Array
You can find the sum of all elements in an array by following the approach below:
- Initialize a variable sum to store the total sum of all elements of the array.
- Traverse the array and add each element of the array with the sum variable.
- Finally, return the sum variable.
C++ Program to Find the Sum of All Elements in an Array
Below is the C++ program to find the sum of all elements in an array:
// C++ program to find the sum of elements in an array
#include
using namespace std;
// Function to return the sum of elements in an array
int findSum(int arr[], int size)
{
int sum = 0;
for(int i=0; i {
sum += arr[i];
}
return sum;
}
// Function to print the elements of the array
void printArray(int arr[], int size)
{
for(int i=0; i {
cout }
cout }
// Driver code
int main()
{
int arr1[] = {1, 2, 3, 4, 5};
int size1 = sizeof(arr1) / sizeof(arr1[0]);
cout printArray(arr1, size1);
cout int arr2[] = {34, 56, 10, -2, 5, 99};
int size2 = sizeof(arr2) / sizeof(arr2[0]);
cout printArray(arr2, size2);
cout int arr3[] = {-1, 50, -56, 43, 53, 356, -324};
int size3 = sizeof(arr3) / sizeof(arr3[0]);
cout printArray(arr3, size3);
cout return 0;
}
Output:
Array 1:
1 2 3 4 5
Sum of elements of the array: 15
Array 2:
34 56 10 -2 5 99
Sum of elements of the array: 202
Array 3:
-1 50 -56 43 53 356 -324
Sum of elements of the array: 121
C++ Program Using STL to Find the Sum of All Elements in an Array
You can also use C++ STL to find the sum of all elements in an array.
// C++ program using STL to find the sum of elements in an array
#include
using namespace std;
// Function to print the elements of the array
void printArray(int arr[], int size)
{
for(int i=0; i {
cout }
cout }
// Driver code
int main()
{
int arr1[] = {1, 2, 3, 4, 5};
int size1 = sizeof(arr1) / sizeof(arr1[0]);
cout printArray(arr1, size1);
cout int arr2[] = {34, 56, 10, -2, 5, 99};
int size2 = sizeof(arr2) / sizeof(arr2[0]);
cout printArray(arr2, size2);
cout int arr3[] = {-1, 50, -56, 43, 53, 356, -324};
int size3 = sizeof(arr3) / sizeof(arr3[0]);
cout printArray(arr3, size3);
cout return 0;
}
Output:
Array 1:
1 2 3 4 5
Sum of elements of the array: 15
Array 2:
34 56 10 -2 5 99
Sum of elements of the array: 202
Array 3:
-1 50 -56 43 53 356 -324
Sum of elements of the array: 121
Python Program to Find the Sum of All Elements in an Array
Below is the Python program to find the sum of all elements in an array:
# Python program to find the sum of elements in an array
# Function to return the sum of elements in an array
def findSum(arr):
sum = 0
for element in arr:
sum += element
return sum
# Function to print the elements of the array
def printArray(arr):
for i in range(len(arr)):
print(arr[i] , end=' ')
print()
# Driver Code
arr1 = [1, 2, 3, 4, 5]
print("Array 1:")
printArray(arr1)
print("Sum of elements of the array:",findSum(arr1))
arr2 = [34, 56, 10, -2, 5, 99]
print("Array 2:")
printArray(arr2)
print("Sum of elements of the array:",findSum(arr2))
arr3 = [-1, 50, -56, 43, 53, 356, -324]
print("Array 3:")
printArray(arr3)
print("Sum of elements of the array:",findSum(arr3))
Output:
Array 1:
1 2 3 4 5
Sum of elements of the array: 15
Array 2:
34 56 10 -2 5 99
Sum of elements of the array: 202
Array 3:
-1 50 -56 43 53 356 -324
Sum of elements of the array: 121
Python Program Using Built-in Function to Find the Sum of All Elements in an Array
You can also use Python’s sum() function to find the sum of all elements in an array.
# Python program to find the sum of elements in an array
# Function to print the elements of the array
def printArray(arr):
for i in range(len(arr)):
print(arr[i] , end=' ')
print()
# Driver Code
arr1 = [1, 2, 3, 4, 5]
print("Array 1:")
printArray(arr1)
print("Sum of elements of the array:",sum(arr1))
arr2 = [34, 56, 10, -2, 5, 99]
print("Array 2:")
printArray(arr2)
print("Sum of elements of the array:",sum(arr2))
arr3 = [-1, 50, -56, 43, 53, 356, -324]
print("Array 3:")
printArray(arr3)
print("Sum of elements of the array:",sum(arr3))
Output:
Array 1:
1 2 3 4 5
Sum of elements of the array: 15
Array 2:
34 56 10 -2 5 99
Sum of elements of the array: 202
Array 3:
-1 50 -56 43 53 356 -324
Sum of elements of the array: 121
JavaScript Program to Find the Sum of All Elements in an Array
Below is the JavaScript program to find the sum of all elements in an array:
// JavaScript program to find the sum of elements in an array
// Function to return the sum of elements in an array
function findSum(arr, size)
{
let sum = 0;
for(let i=0; i {
sum += arr[i];
}
return sum;
}
// Function to print the elements of the array
function printArray(arr, size)
{
for(let i=0; i {
document.write(arr[i] + " ");
}
document.write("
");
}
// Driver code
const arr1 = [1, 2, 3, 4, 5]
size1 = arr1.length;
document.write("Array 1:
");
printArray(arr1, size1);
document.write("Sum of elements of the array: " + findSum(arr1, size1) + "
");
const arr2 = [34, 56, 10, -2, 5, 99]
size2 = arr2.length;
document.write("Array 2:
");
printArray(arr2, size2);
document.write("Sum of elements of the array: " + findSum(arr2, size2) + "
");
const arr3 = [-1, 50, -56, 43, 53, 356, -324]
size3 = arr3.length;
document.write("Array 3:
");
printArray(arr3, size3);
document.write("Sum of elements of the array: " + findSum(arr3, size3) + "
");
Output:
Array 1:
1 2 3 4 5
Sum of elements of the array: 15
Array 2:
34 56 10 -2 5 99
Sum of elements of the array: 202
Array 3:
-1 50 -56 43 53 356 -324
Sum of elements of the array: 121
JavaScript Program Using the reduce() Method to Find the Sum of All Elements in an Array
You can also use JavaScript’s reduce() method to find the sum of all elements in an array.
// JavaScript program to find the sum of elements in an array
// Function to print the elements of the array
function printArray(arr, size)
{
for(let i=0; i {
document.write(arr[i] + " ");
}
document.write("
");
}
// Driver code
const arr1 = [1, 2, 3, 4, 5]
size1 = arr1.length;
document.write("Array 1:
");
printArray(arr1, size1);
var sum1 = arr1.reduce(function(a, b) { return a + b; }, 0);
document.write("Sum of elements of the array: " + sum1 + "
");
const arr2 = [34, 56, 10, -2, 5, 99]
size2 = arr2.length;
document.write("Array 2:
");
printArray(arr2, size2);
var sum2 = arr2.reduce(function(a, b) { return a + b; }, 0);
document.write("Sum of elements of the array: " + sum2 + "
");
const arr3 = [-1, 50, -56, 43, 53, 356, -324]
size3 = arr3.length;
document.write("Array 3:
");
printArray(arr3, size3);
var sum3 = arr3.reduce(function(a, b) { return a + b; }, 0);
document.write("Sum of elements of the array: " + sum3 + "
");
Output:
Array 1:
1 2 3 4 5
Sum of elements of the array: 15
Array 2:
34 56 10 -2 5 99
Sum of elements of the array: 202
Array 3:
-1 50 -56 43 53 356 -324
Sum of elements of the array: 121
Want to Learn C++?
C++ is among the most popular programming languages. You can use C++ for basic programming, developing games, developing GUI-based applications, developing database software, developing operating systems, and much more.
If you’re a beginner to C++ or want to revise your C++ concepts, check out some of the top websites and courses to get you started.