How to Find the Sum of Elements in an Array in C++? Last Updated : 26 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, an array is the collection of similar data elements that are stored in the contiguous memory location and we can access these elements directly by their index value. In this article, we will learn how to find the sum of elements in an array in C++. Example:Input: myVector = {1, 2, 3, 4, 5} Output: Sum = 15Find the Sum of Values in an Array in C++We can find the sum of all elements in an array in C++ by using a loop and keep adding each element to the collective sum: ApproachCreate a variable named sum and initialize it with 0.Run a loop starting from index 0 to N-1.Inside the loop, in each iteration, add the current array element to the variable.When the loop terminates, the sum variable will contain the sum of all the values in the array.C++ Program to Find the Sum of Values in an Array C++ // C++ program to find the sum of all elements in an array #include <iostream> using namespace std; int main() { // Initialize the array int arr[] = { 1, 2, 3, 4, 5 }; // size of the array int n = sizeof(arr) / sizeof(arr[0]); // sum of the array elements int sum = 0; for (int i = 0; i < n; i++) sum += arr[i]; cout << "Sum: " << sum << endl; return 0; } OutputSum: 15 Time Complexity: O(N), where n is the number of elements in the array.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Find the Sum of Elements in an Array in C++? A abhishek9202 Follow Improve Article Tags : C++ Programs C++ cpp-array CPP Examples Practice Tags : CPP Similar Reads How to find the sum of elements of an Array using STL in C++? Given an array arr[], find the sum of the elements of this array using STL in C++. Example: Input: arr[] = {1, 45, 54, 71, 76, 12} Output: 259 Input: arr[] = {1, 7, 5, 4, 6, 12} Output: 35 Approach: Sum can be found with the help of accumulate() function provided in STL. Syntax: accumulate(first_ind 1 min read How to Find the Sum of All Elements in a Deque in C++? In C++, double-ended queues are sequence containers similar to vectors but are more efficient in the case of insertion and deletion of elements at both ends. In this article, we will learn how to find the sum of all elements in a deque in C++. Example Input: myDeque ={1,2,3,4,5} Output: Sum of all d 2 min read How to Find the Second Smallest Element in an Array in C++? In C++, arrays are data structures that store the collection of data elements of the same type in contiguous memory locations. In this article, we will learn how to find the second smallest element in an array in C++. Example:Input:myArray = {10, 5, 8, 2, 7, 3, 15};Output:The second smallest element 3 min read How to Find the Unique Elements in an Array in C++? In C++, an array is a data structure that is used to store multiple values of similar data types in a contiguous memory location. In this article, we will learn how to find the unique elements in an array in C++. Example: Input: array = {1, 2, 1, 2, 2, 3, 4} Output: Unique elements in the array: 1 2 2 min read How to Find the Sum of All Elements in a List in C++ STL? In C++, a list is a sequence container provided by the STL library that represents a doubly linked list and allows to store data in non-contiguous memory locations. In this article, we will learn how to find the sum of all elements in a list in C++. Example: Input:myList = {10, 20, 30, 40, 50};Outpu 2 min read Like