valarray sum() in C++ Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report The sum() function is defined in valarray header file. This function returns the sum of all the elements in the valarray, as if calculated by applying operator+= to a copy of one element and all the other elements, in an unspecified order. Syntax: T sum() const; Returns: This function returns the sum of all the elements in the valarray. Below programs illustrate the above function: Example 1:- CPP // C++ program to demonstrate // example of sum() function. #include <bits/stdc++.h> using namespace std; int main() { // Initializing valarray valarray<int> varr = { 15, 10, 30, 33, 40 }; // Displaying sum of valarray cout << "The sum of valarray is = " << varr.sum() << endl; return 0; } Output: The sum of valarray is = 128 Example 2:- CPP // C++ program to demonstrate // example of sum() function. #include <bits/stdc++.h> using namespace std; int main() { // Initializing valarray valarray<int> varr = { 1, 2, 3, 4, 5 }; // Displaying sum of valarray cout << "The sum of valarray is = " << varr.sum() << endl; return 0; } Output: The sum of valarray is = 15 Comment More infoAdvertise with us Next Article Add Two Numbers in C++ B bansal_rtk_ Follow Improve Article Tags : Misc C++ Programs C++ CPP-Functions cpp-valarray +1 More Practice Tags : CPPMisc Similar Reads valarray size() function in C++ The size() function is defined in valarray header file. This function is used to find the size of valarray and returns the size of valarray. Syntax: size_t size() const; Parameter: This function doesn't takes any parameter. Returns: This function returns the number of element in valarray. Below prog 1 min read Array sum in C++ STL Array sum can be defined as the sum of all elements of an array. In this article, we will learn how to find the array sum using C++ STL.ExamplesInput: arr[] = {5, 10, 15, 11, 9}Output: 50Explanation: As 5 + 10 + 15 + 11 + 19 = 50Input: arr[] = {1, 2, 3, 4, 5}Output: 15Explanation: As 1 + 2 + 3 + 4 + 2 min read Add Two Numbers in C++ Given two integers, the task is to add these integer number and print their sum in C++.ExamplesInput: a = 11, b = 9Output: 20Explanation: Sum of 11 + 9 = 20Input: a = 1, b = 8Output: 9Explanation: Sum of 1 + 8 = 9 Add Two Numbers Using Addition OperatorIn C++, the simplest method for adding the two 3 min read Sum of all odd length subarrays Given an array arr[] consisting of N integers, the task is to find the sum of all the elements of all possible subarrays of odd length. Examples: Input: arr[] = {3, 2, 4}Output: 18Explanation:The odd length subarrays along with their sum are as follows:1) {3} = sum is 3.2) {2} = sum is 2.3) {4} = su 9 min read C/C++ Programs sArray C/C++ ProgramsC Program to find sum of elements in a given arrayC program to find largest element in an arrayRecursive C program to linearly search an element in a given arrayC program to multiply two matricesC/C++ Program for Given an array A[] and a number x, check for pair in A[] with sum 15+ min read C++ Program to Count pairs with given sum Given an array of integers, and a number 'sum', find the number of pairs of integers in the array whose sum is equal to 'sum'. Examples: Input : arr[] = {1, 5, 7, -1}, sum = 6 Output : 2 Pairs with sum 6 are (1, 5) and (7, -1) Input : arr[] = {1, 5, 7, -1, 5}, sum = 6 Output : 3 Pairs with sum 6 are 4 min read Like