How to multiply all the elements of a std::array in C++

1 Answer

0 votes
#include <iostream>
#include <array>
#include <numeric> // std::accumulate

int main() {
    std::array<int, 5> arr = {1, 2, 3, 4, 5};

    // Multiply all elements using std::accumulate
    int product = std::accumulate(arr.begin(), arr.end(), 1, std::multiplies<int>());

    std::cout << "The product of all elements is: " << product << std::endl;
}



/*
run:

The product of all elements is: 120

*/

 



answered Jun 20, 2025 by avibootz
...