
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
How to find the sum of elements of an Array using STL in C++?
Here we will see how to find the sum of all element of an array. So if the array is like [12, 45, 74, 32, 66, 96, 21, 32, 27], then sum will be: 405. So here we have to use the accumulate() function to solve this problem. This function description is present inside <numeric> header file.
Example
#include<iostream> #include<numeric> using namespace std; int main() { int arr[] = {12, 45, 74, 32, 66, 96, 21, 32, 27}; int n = sizeof(arr) / sizeof(arr[0]); cout << "Array is like: "; for (int i = 0; i < n; i++) cout << arr[i] << " "; cout << "\nSum of all elements: " << accumulate(arr, arr + n, 0); }
Output
Array is like: 12 45 74 32 66 96 21 32 27 Sum of all elements: 405
- Related Articles
- How to find the sum of elements of a Vector using STL in C++?
- How to find the maximum element of an Array using STL in C++?
- Find elements of an array which are divisible by N using STL in C++
- Find elements of an Array which are Odd and Even using STL in C++
- C Program to find sum of perfect square elements in an array using pointers.
- How to find the minimum and maximum element of an Array using STL in C++?
- How to reverse an Array using STL in C++?
- How to sort an Array using STL in C++?
- Java program to find the sum of elements of an array
- How to calculate sum of array elements using pointers in C language?
- How to find the average of elements of an integer array in C#?
- How to find the sum of all array elements in R?
- Array sum in C++ STL
- C/C++ Program to find the sum of elements in a given array
- Find an element in array such that sum of left array is equal to sum of right array using c++

Advertisements