Array Product in C++ Using STL



The product of an array refers to the multiplication result of all the elements in the array. The STL library of C++ provides several in-built functions to quickly calculate product of an array. In this article, we will explain all those STL functions with examples. First of all, let's understand our problem statement:

In this problem, you are given an array of integers, and you need to find the product of all the elements in the array. For example:

// Input array
int arr[] = {1, 2, 3, 4, 5};

// Output
Product = 120

// Explanation: 1 * 2 * 3 * 4 * 5 = 120

Array Product Using STL

Here is the list of all the approaches to find the product of array elements using c++ STL library, which we will be discussing in this article with stepwise explanation and complete example codes

Using accumulate Function

The accumulate function is a part STL library defined in the numeric header file. This can be used sum, difference, product, etc. of elements in a range of array. The syntax of the accumulate function for finding product is as follows:

int res = accumulate(arr, arr + n, 1, multiplies<int>());

Where arr is the pointer to the first element of the array, n is the size of the array, and 1 is the initial value for multiplication. The multiplies<int>() is a function object that performs multiplication. Now, let's see an example to understand how to use this function.

Example

The code below implements the accumulate function to find the product of all elements in an array:

#include <iostream>
#include <numeric>

using namespace std;

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n  = sizeof(arr)/sizeof(arr[0]);
 
    // Finding product of all elements 
    int res = accumulate(arr, arr + n, 1, multiplies<int>());

    cout << res;
    return 0;
}

The output of the above code will be:

120

Using reduce Function

The reduce function is another function from the STL library that can be used to find the product of elements in an array. It is also defined in the numeric header file. The syntax of the reduce function is similar to that of accumulate function:

int res = reduce(arr, arr + n, 1, multiplies());

Now, let's see an example to understand how to use this function.

Example

The code below implements the reduce function to find the product of all elements in an array:

#include <iostream>
#include <numeric>

using namespace std;

int main() {
    int arr[] = {1, 2, 3, 2};
    int n  = sizeof(arr)/sizeof(arr[0]);
 
    // Finding product of all elements 
    int res = reduce(arr, arr + n, 1, multiplies<int>());

    cout << res;
    return 0;
}

The output of the above code will be:

12

Using for_each Function

The for_each function is another useful function from the STL library that can be used to iterate through the elements of an array and perform operations on them. The syntax of the for_each function is as follows:

for_each(arr, arr + n, [](int &x) { x *= 2; });

Where arr is the pointer to the first element of the array, n is the size of the array, and the lambda function performs the multiplication operation on each element of the array. Now, let's see an example to understand how to use this function.

Example

The code below implements the for_each function to find the product of all elements in an array:

#include <iostream>
#include <algorithm>
#include <numeric>

using namespace std;

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n  = sizeof(arr)/sizeof(arr[0]);
 
    // Finding product of all elements 
    int product = 1;
    for_each(arr, arr + n, [&product](int x) { product *= x; });

    cout << product;
    return 0;
}

The output of the above code will be:

120
Updated on: 2025-05-28T17:36:55+05:30

250 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements