An array is a collection of elements stored at contiguous memory locations. It’s the most used data structure in programming. You must know how to perform basic operations on an array, like insertion, deletion, traversal, finding the sum of all elements, finding the product of all elements, etc.
In this article, you’ll learn how to find the product of all elements in an array using iterative and recursive approaches.
Problem Statement
You’re given an array arr. You need to find the product of all elements of the array, then print the final product. You need to implement this solution using loops and recursion.
Example 1: Let arr = [1, 2, 3, 4, 5, 6, 7, 8]
The product of each element of the array = 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 = 40320
Thus, the output is 40320.
Example 2: Let arr = [1, 1, 1, 1, 1, 1]
The product of each element of the array = 1 * 1 * 1 * 1 * 1 * 1 = 1
Thus, the output is 1.
Iterative Approach to Find the Product of All Elements of the Array
You can find the product of all elements of the array using iteration/loops by following the approach below:
- Initialize a variable result (with a value of 1) to store the product of all elements in the array.
- Iterate through the array and multiply each element of the array with the result.
- Finally, return the result.
C++ Program to Find the Product of Array Elements Using Loops
Below is the C++ program to find the product of array elements:
// C++ program to find the product of the array elements
#include <iostream>
using namespace std;
int findProduct(int arr[], int size)
{
int result = 1;
for(int i=0; i<size; i++)
{
result = result * arr[i];
}
return result;
}void printArrayElements(int arr[], int size)
{
for(int i=0; i<size; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
int main()
{
int arr1[] = {1, 2, 3, 4, 5, 6, 7, 8};
int size1 = sizeof(arr1)/sizeof(arr1[0]);
cout << "Array 1:" << endl;
printArrayElements(arr1, size1);
cout << "Product of the array elements: " << findProduct(arr1, size1) << endl;
int arr2[] = {1, 1, 1, 1, 1, 1};
int size2 = sizeof(arr2)/sizeof(arr2[0]);
cout << "Array 2:" << endl;
printArrayElements(arr2, size2);
cout << "Product of the array elements: " << findProduct(arr2, size2) << endl;
return 0;
}
Output:
Array 1:
1 2 3 4 5 6 7 8
Product of the array elements: 40320
Array 2:
1 1 1 1 1 1
Product of the array elements: 1
Python Program to Find the Product of Array Elements Using Loops
Below is the Python program to find the product of array elements:
# Python program to find product of the list elements
def findProduct(arr, size):
result = 1
for i in range(size):
result = result * arr[i]
return result
def printListElements(arr, size):
for i in range(size):
print(arr[i], end=" ")
print()
arr1 = [1, 2, 3, 4, 5, 6, 7, 8]
size1 = len(arr1)
print("Array 1:")
printListElements(arr1, size1)
print("Product of the array elements:", findProduct(arr1, size1))
arr2 = [1, 1, 1, 1, 1, 1]
size2 = len(arr2)
print("Array 2:")
printListElements(arr2, size2)
print("Product of the array elements:", findProduct(arr2, size2))
Output:
Array 1:
1 2 3 4 5 6 7 8
Product of the array elements: 40320
Array 2:
1 1 1 1 1 1
Product of the array elements: 1
JavaScript Program to Find the Product of Array Elements Using Loops
Below is the JavaScript program to find the product of array elements:
// JavaScript program to find the product of the array elements
function findProduct(arr, size) {
let result = 1;
for(let i=0; i<size; i++) {
result = result * arr[i];
}
return result;
}
function printArrayElements(arr, size) {
for(let i=0; i<size; i++) {
document.write(arr[i] + " ");
}
document.write("<br>");
}var arr1 = [1, 2, 3, 4, 5, 6, 7, 8];
var size1 = arr1.length;
document.write("Array 1:" + "<br>");
printArrayElements(arr1, size1);
document.write("Product of the array elements: " + findProduct(arr1, size1) + "<br>");
var arr2 = [1, 1, 1, 1, 1, 1];
var size2 = arr2.length;
document.write("Array 2:" + "<br>");
printArrayElements(arr2, size2);
document.write("Product of the array elements: " + findProduct(arr2, size2) + "<br>");
Output:
Array 1:
1 2 3 4 5 6 7 8
Product of the array elements: 40320
Array 2:
1 1 1 1 1 1
Product of the array elements: 1
C Program to Find the Product of Array Elements Using Loops
Below is the C program to find the product of array elements:
// C program to find the product of the array elements
#include <stdio.h>
int findProduct(int arr[], int size)
{
int result = 1;
for(int i=0; i<size; i++)
{
result = result * arr[i];
}
return result;
}void printArrayElements(int arr[], int size)
{
for(int i=0; i<size; i++)
{
printf("%d ", arr[i]);
}
printf("n");
}
int main()
{
int arr1[] = {1, 2, 3, 4, 5, 6, 7, 8};
int size1 = sizeof(arr1)/sizeof(arr1[0]);
printf("Array 1: n");
printArrayElements(arr1, size1);
printf("Product of the array elements: %d n", findProduct(arr1, size1));
int arr2[] = {1, 1, 1, 1, 1, 1};
int size2 = sizeof(arr2)/sizeof(arr2[0]);
printf("Array 2: n");
printArrayElements(arr2, size2);
printf("Product of the array elements: %d n", findProduct(arr2, size2));
return 0;
}
Output:
Array 1:
1 2 3 4 5 6 7 8
Product of the array elements: 40320
Array 2:
1 1 1 1 1 1
Product of the array elements: 1
Recursive Approach to Find the Product of All Elements in an Array
You can find the product of all elements of the array using recursion by following the pseudocode below:
function findProduct(arr,n):
if n == 0:
return(arr[n])
else:
return (arr[n] * findProduct(arr, n - 1))
C++ Program to Find the Product of Array Elements Using Recursion
Below is the C++ program to find the product of array elements:
// C++ program to find the product of the array elements using recursion
#include <iostream>
using namespace std;
int findProduct(int arr[], int n)
{
if (n == 0)
{
return(arr[n]);
}
else
{
return (arr[n] * findProduct(arr, n - 1));
}
}void printArrayElements(int arr[], int size)
{
for(int i=0; i<size; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
int main()
{
int arr1[] = {1, 2, 3, 4, 5, 6, 7, 8};
int size1 = sizeof(arr1)/sizeof(arr1[0]);
cout << "Array 1:" << endl;
printArrayElements(arr1, size1);
cout << "Product of the array elements: " << findProduct(arr1, size1-1) << endl;
int arr2[] = {1, 1, 1, 1, 1, 1};
int size2 = sizeof(arr2)/sizeof(arr2[0]);
cout << "Array 2:" << endl;
printArrayElements(arr2, size2);
cout << "Product of the array elements: " << findProduct(arr2, size2-1) << endl;
return 0;
}
Output:
Array 1:
1 2 3 4 5 6 7 8
Product of the array elements: 40320
Array 2:
1 1 1 1 1 1
Product of the array elements: 1
Python Program to Find the Product of Array Elements Using Recursion
Below is the Python program to find the product of array elements:
# Python program to find th eproduct of the list elements using recursion
def findProduct(arr, n):
if n == 0:
return(arr[n])
else:
return (arr[n] * findProduct(arr, n - 1))
def printListElements(arr, size):
for i in range(size):
print(arr[i], end=" ")
print()
arr1 = [1, 2, 3, 4, 5, 6, 7, 8]
size1 = len(arr1)
print("Array 1:")
printListElements(arr1, size1)
print("Product of the array elements:", findProduct(arr1, size1-1))
arr2 = [1, 1, 1, 1, 1, 1]
size2 = len(arr2)
print("Array 2:")
printListElements(arr2, size2)
print("Product of the array elements:", findProduct(arr2, size2-1))
Output:
Array 1:
1 2 3 4 5 6 7 8
Product of the array elements: 40320
Array 2:
1 1 1 1 1 1
Product of the array elements: 1
JavaScript Program to Find the Product of Array Elements Using Recursion
Below is the JavaScript program to find the product of array elements:
// JavaScript program to find the product of the array elements using recursion
function findProduct(arr, n) {
if (n == 0) {
return(arr[n]);
} else {
return (arr[n] * findProduct(arr, n - 1));
}
}
function printArrayElements(arr, size) {
for(let i=0; i<size; i++) {
document.write(arr[i] + " ");
}
document.write("<br>");
}var arr1 = [1, 2, 3, 4, 5, 6, 7, 8];
var size1 = arr1.length;
document.write("Array 1:" + "<br>");
printArrayElements(arr1, size1);
document.write("Product of the array elements: " + findProduct(arr1, size1) + "<br>");
var arr2 = [1, 1, 1, 1, 1, 1];
var size2 = arr2.length;
document.write("Array 2:" + "<br>");
printArrayElements(arr2, size2);
document.write("Product of the array elements: " + findProduct(arr2, size2) + "<br>");
Output:
Array 1:
1 2 3 4 5 6 7 8
Product of the array elements: 40320
Array 2:
1 1 1 1 1 1
Product of the array elements: 1
C Program to Find the Product of Array Elements Using Recursion
Below is the C program to find the product of array elements:
// C program to find the product of the array elements using recursion
#include <stdio.h>
int findProduct(int arr[], int n)
{
if (n == 0)
{
return(arr[n]);
}
else
{
return (arr[n] * findProduct(arr, n - 1));
}
}void printArrayElements(int arr[], int size)
{
for(int i=0; i<size; i++)
{
printf("%d ", arr[i]);
}
printf("n");
}
int main()
{
int arr1[] = {1, 2, 3, 4, 5, 6, 7, 8};
int size1 = sizeof(arr1)/sizeof(arr1[0]);
printf("Array 1: n");
printArrayElements(arr1, size1);
printf("Product of the array elements: %d n", findProduct(arr1, size1-1));
int arr2[] = {1, 1, 1, 1, 1, 1};
int size2 = sizeof(arr2)/sizeof(arr2[0]);
printf("Array 2: n");
printArrayElements(arr2, size2);
printf("Product of the array elements: %d n", findProduct(arr2, size2-1));
return 0;
}
Output:
Array 1:
1 2 3 4 5 6 7 8
Product of the array elements: 40320
Array 2:
1 1 1 1 1 1
Product of the array elements: 1
Strengthen Your Array Concepts
Arrays are an integral part of programming. They’re one of the most important topics for technical interviews as well.
If programs based on arrays still scare you, try solving some basic array problems like how to find the sum of all elements in an array, how to find the maximum and minimum element in an array, how to reverse an array, etc. It’ll help you to strengthen your array concepts.
Read Next
About The Author