How to Find the Cumulative Sum of Array in C++? Last Updated : 19 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, the cumulative sum, also known as the prefix sum of an array is the sum of all elements of the array at the current index including the sum of the previous elements. In this article, we will learn how to find the prefix sum of elements in an array in C++. Example Input: arr[]= {1,2,3,4,5} Output: Cumulative sum ={1, 3, 6, 10, 15}Find the Prefix Sum of Elements in an Array in C++To find the cumulative sum of the elements in an array, we can use a simple for loop to iterate through the array and update each element to be the sum of itself and all previous elements but make sure you are using the proper data type to store the sum so that it doesn't overflow. Prefer to take a long long to be on the safe side. C++ Program to Find the Cumulative Sum of Elements in an ArrayThe below program illustrates how we can find the cumulative sum of elements in an array in C++. C++ // C++ Program to illustrate find the prefix sum of elements // in an array #include <iostream> using namespace std; int main() { // Define the array of integers int array[] = { 1, 2, 3, 4, 5 }; // Calculate the size of the array int n = sizeof(array) / sizeof(array[0]); // Declare an array to store the cumulative sum int cum_sum[n]; // Calculate the cumulative sum cum_sum[0] = array[0]; for (int i = 1; i < n; i++) cum_sum[i] = cum_sum[i - 1] + array[i]; // Output the cumulative sum for (int i = 0; i < n; i++) cout << cum_sum[i] << " "; return 0; } Output1 3 6 10 15 Time Complexity: O(N), here N denotes the size of the array.Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article How to Find the Cumulative Product of Elements in an Array in C++? G gaurav472 Follow Improve Article Tags : C++ Programs C++ cpp-array CPP Examples Practice Tags : CPP Similar Reads How to Find the Cumulative Product of Elements in an Array in C++? In C++, an array is a data structure that stores elements of the same type in contiguous memory locations. The cumulative product of an array is the result of multiplying all the elements in the array. In this article, we will learn how to find the cumulative product of elements in an array in C++. 2 min read How to Find the Sum of Elements in an Array in C++? 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} O 2 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 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 Length of an Array in C++? In C++, the length of an array is defined as the total number of elements present in the array. In this article, we will learn how to find the length of an array in C++.The simplest way to find the length of an array is by using the sizeof operator. First calculate the size of the array in bytes and 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