Program for product of array
Last Updated :
20 Sep, 2024
Given an array, find a product of all array elements.
Examples :
Input : ar[] = {1, 2, 3, 4, 5}
Output : 120
Product of array elements is 1 x 2
x 3 x 4 x 5 = 120.
Input : ar[] = {1, 6, 3}
Output : 18
Iterative Approach
- Initialize result as 1
- One by one traverse array elements and multiply with the result
- Return result
C++
// C++ program to find product of array elements.
#include <iostream>
using namespace std;
int product(int ar[], int n)
{
int result = 1;
for (int i = 0; i < n; i++)
result = result * ar[i];
return result;
}
int main()
{
int ar[] = { 1, 2, 3, 4, 5 };
int n = sizeof(ar) / sizeof(ar[0]);
cout << product(ar, n);
return 0;
}
// This code is contributed by lokeshmvs21.
C
#include <stdio.h>
// Function to find product of array elements
int product(int ar[], int n) {
int result = 1;
for (int i = 0; i < n; i++)
result = result * ar[i];
return result;
}
int main() {
int ar[] = { 1, 2, 3, 4, 5 };
int n = sizeof(ar) / sizeof(ar[0]);
printf("%d\n", product(ar, n));
return 0;
}
// This code is contributed by lokeshmvs21.
Java
class GfG {
// Function to find product of array elements
static int product(int[] ar, int n) {
int result = 1;
for (int i = 0; i < n; i++)
result = result * ar[i];
return result;
}
public static void main(String[] args) {
int[] ar = { 1, 2, 3, 4, 5 };
int n = ar.length;
System.out.println(product(ar, n));
}
}
// This code is contributed by lokeshmvs21.
Python
# Function to find product of array elements
def product(ar, n):
result = 1
for i in range(n):
result *= ar[i]
return result
# Driver code
if __name__ == "__main__":
ar = [1, 2, 3, 4, 5]
n = len(ar)
print(product(ar, n))
# This code is contributed by lokeshmvs21.
C#
using System;
class GfG
{
// Function to find product of array elements
static int Product(int[] ar, int n)
{
int result = 1;
for (int i = 0; i < n; i++)
result = result * ar[i];
return result;
}
// Driver code
static void Main()
{
int[] ar = { 1, 2, 3, 4, 5 };
int n = ar.Length;
Console.WriteLine(Product(ar, n));
}
}
// This code is contributed by lokeshmvs21.
JavaScript
// Function to find product of array elements
function product(ar, n) {
let result = 1;
for (let i = 0; i < n; i++) {
result = result * ar[i];
}
return result;
}
// Driver code
let ar = [1, 2, 3, 4, 5];
let n = ar.length;
console.log(product(ar, n));
// This code is contributed by lokeshmvs21.
Time Complexity : O(n)
Auxiliary Space : O(1)
Recursive Approach
We can recursively define the problem as
product(arr, n) = arr[n-1) x product(arr, n-1)
With base cases
product(arr, 1) = arr[0]
C++
#include <iostream>
using namespace std;
// Recursive function to find product
// of array elements
int product(int ar[], int n)
{
// Base case: if the array has only
// one element, return it
if (n == 1)
return ar[0];
// Recursive case: multiply the last
// element with the product of the
// remaining array
return ar[n - 1] * product(ar, n - 1);
}
int main()
{
int ar[] = {1, 2, 3, 4, 5};
int n = sizeof(ar) / sizeof(ar[0]);
cout << product(ar, n);
return 0;
}
C
#include <stdio.h>
// Recursive function to find product
// of array elements
int product(int ar[], int n) {
// Base case: if the array has only
// one element, return it
if (n == 1)
return ar[0];
// Recursive case: multiply the last
// element with the product of the
// remaining array
return ar[n - 1] * product(ar, n - 1);
}
int main() {
int ar[] = {1, 2, 3, 4, 5};
int n = sizeof(ar) / sizeof(ar[0]);
printf("%d\n", product(ar, n));
return 0;
}
Java
class GfG {
// Recursive function to find product
// of array elements
static int product(int[] ar, int n) {
// Base case: if the array has only
// one element, return it
if (n == 1)
return ar[0];
// Recursive case: multiply the last
// element with the product of the
// remaining array
return ar[n - 1] * product(ar, n - 1);
}
public static void main(String[] args) {
int[] ar = {1, 2, 3, 4, 5};
int n = ar.length;
System.out.println(product(ar, n));
}
}
Python
# Recursive function to find product
# of array elements
def product(ar, n):
# Base case: if the array has only
# one element, return it
if n == 1:
return ar[0]
# Recursive case: multiply the last
# element with the product of the
# remaining array
return ar[n - 1] * product(ar, n - 1)
# Driver code
if __name__ == "__main__":
ar = [1, 2, 3, 4, 5]
n = len(ar)
print(product(ar, n))
C#
using System;
class GfG
{
// Recursive function to find product
// of array elements
static int Product(int[] ar, int n)
{
// Base case: if the array has only
// one element, return it
if (n == 1)
return ar[0];
// Recursive case: multiply the last
// element with the product of the
// remaining array
return ar[n - 1] * Product(ar, n - 1);
}
static void Main()
{
int[] ar = {1, 2, 3, 4, 5};
int n = ar.Length;
Console.WriteLine(Product(ar, n));
}
}
JavaScript
// Recursive function to find product
// of array elements
function product(ar, n) {
// Base case: if the array has only
// one element, return it
if (n === 1) {
return ar[0];
}
// Recursive case: multiply the last
// element with the product of the
// remaining array
return ar[n - 1] * product(ar, n - 1);
}
// Driver code
const ar = [1, 2, 3, 4, 5];
const n = ar.length;
console.log(product(ar, n));
Time Complexity : O(n)
Auxiliary Space : O(1)
Which one is better?
Iterative approach is better as the recursive approach requires extra space for recursion call stack and overhead of recursion calls. However writing a recursive code is always a fun exercise.
Handling Overflows with Modulo Arithmetic
The above codes may cause overflow. Therefore, it is always desired to compute product under modulo. The reason for its working is the simple distributive property of modulo.
( a * b) % c = ( ( a % c ) * ( b % c ) ) % c
Below is a program to find and print the product of all the number in this array of Modulo (10^9 +7).
C++
// C++ code for above program to find product
// under modulo.
#include <iostream>
using namespace std;
const int MOD = 1000000007;
int product(int ar[], int n)
{
int result = 1;
for (int i = 0; i < n; i++)
result = (result * ar[i]) % MOD;
return result;
}
int main()
{
int ar[] = { 1, 2, 3, 4, 5 };
int n = sizeof(ar) / sizeof(ar[0]);
cout << product(ar, n);
return 0;
}
// This code is contributed by lokeshmvs21.
C
#include <stdio.h>
const int MOD = 1000000007;
// Function to find product of array elements under modulo
int product(int ar[], int n) {
int result = 1;
for (int i = 0; i < n; i++)
result = (result * ar[i]) % MOD;
return result;
}
int main() {
int ar[] = {1, 2, 3, 4, 5};
int n = sizeof(ar) / sizeof(ar[0]);
printf("%d\n", product(ar, n));
return 0;
}
// This code is contributed by lokeshmvs21.
Java
class GfG {
// Modulo constant
static final int MOD = 1000000007;
// Function to find product of array elements under modulo
static int product(int[] ar, int n) {
int result = 1;
for (int i = 0; i < n; i++)
result = (int) ((result * (long) ar[i]) % MOD);
return result;
}
public static void main(String[] args) {
int[] ar = {1, 2, 3, 4, 5};
int n = ar.length;
System.out.println(product(ar, n));
}
}
// This code is contributed by lokeshmvs21.
Python
# Modulo constant
MOD = 1000000007
# Function to find product of array elements under modulo
def product(ar, n):
result = 1
for i in range(n):
result = (result * ar[i]) % MOD
return result
# Driver code
if __name__ == "__main__":
ar = [1, 2, 3, 4, 5]
n = len(ar)
print(product(ar, n))
# This code is contributed by lokeshmvs21.
C#
using System;
class GfG
{
// Modulo constant
const int MOD = 1000000007;
// Function to find product of array elements under modulo
static int Product(int[] ar, int n)
{
int result = 1;
for (int i = 0; i < n; i++)
result = (int)((result * (long)ar[i]) % MOD);
return result;
}
static void Main()
{
int[] ar = { 1, 2, 3, 4, 5 };
int n = ar.Length;
Console.WriteLine(Product(ar, n));
}
}
// This code is contributed by lokeshmvs21.
JavaScript
// Modulo constant
const MOD = 1000000007;
// Function to find product of array elements under modulo
function product(ar, n) {
let result = 1;
for (let i = 0; i < n; i++) {
result = (result * ar[i]) % MOD;
}
return result;
}
// Driver code
let ar = [1, 2, 3, 4, 5];
let n = ar.length;
console.log(product(ar, n));
// This code is contributed by lokeshmvs21.
Time Complexity : O(n)
Auxiliary Space : O(1)
Exercise Problems:
Sign of the product of a Array
Product of Array except itself.
Minimum steps to make the product of the array equal to 1
Similar Reads
Product of all Subarrays of an Array Given an array of integers arr of size N, the task is to print products of all subarrays of the array. Examples: Input: arr[] = {2, 4} Output: 64 Here, subarrays are [2], [2, 4], [4] Products are 2, 8, 4 Product of all Subarrays = 64 Input : arr[] = {10, 3, 7} Output : 27783000 Here, subarrays are [
7 min read
Prefix Product Array Given an array arr[] of N integers the task is to generate a prefix product array from the given array. In a prefix product array, ith term pref[i] = arr[i] * arr[i - 1] * ...... * arr[0] Examples: Input: {1, 2, 3, 4, 5} Output: {1, 2, 6, 24, 120} Explanation: The Prefix Product Array will be {1, 2*
4 min read
Product of Array Except Self Given an array arr[] of n integers, construct a product array res[] (of the same size) such that res[i] is equal to the product of all the elements of arr[] except arr[i]. Example: Input: arr[] = [10, 3, 5, 6, 2]Output: [180, 600, 360, 300, 900]Explanation: For i=0, res[i] = 3 * 5 * 6 * 2 is 180.For
13 min read
PHP | array_product() Function The array_product() is an inbuilt function in PHP and it returns the product of all numbers present in the given array. The function accepts an array which consists of numbers only. If the array has any other data except numbers, the function returns 0. Syntax: array_product($array) Parameters: The
2 min read
PHP array_product() Function There are some point of time when we need to calculate the product of all elements in an array. The most basic way to do this is to iterate over all elements and calculate the product but PHP serves us with a builtin function to do this. The array_product() is a built-in function in PHP and is used
2 min read
Maximum Sum of Products of Two Arrays Given two arrays A and B of positive integers of the same size N. The task is to find the maximum sum of products of their elements. Each element in A has to be multiplied with exactly one element in B or vice versa such that each element of both the arrays appears exactly once and the sum of the pr
5 min read
Range product queries in an array We have an array of integers and a set of range queries. For every query, we need to find product of elements in the given range. Example: Input : arr[] = {5, 10, 15, 20, 25} queries[] = {(3, 5), (2, 2), (2, 3)} Output : 7500, 10, 150 7500 = 15 x 20 x 25 10 = 10 150 = 10 x 15 A naive approach would
10 min read
Product of all non repeating Subarrays of an Array Given an array containing distinct integers arr[] of size N, the task is to print the product of all non-repeating subarrays of the array. Examples: Input: arr[] = {2, 4} Output: 64 Explanation: The possible subarrays for the given array are {2}, {2, 4}, {4} The products are 2, 8, 4 respectively. Th
5 min read
Count number of trailing zeros in product of array Given a array size of n, we need to find the total number of zeros in the product of array. Examples: Input : a[] = {100, 20, 40, 25, 4} Output : 6 Product is 100 * 20 * 40 * 25 * 4 which is 8000000 and has 6 trailing 0s. Input : a[] = {10, 100, 20, 30, 25, 4, 43, 25, 50, 90, 12, 80} Output : 13 A s
6 min read
Program for multiplication of array elements We are given an array, and we have to calculate the product of an array using both iterative and recursive methods. Examples: Input : array[] = {1, 2, 3, 4, 5, 6}Output : 720Here, product of elements = 1*2*3*4*5*6 = 720 Input : array[] = {1, 3, 5, 7, 9}Output : 945 Iterative Method: We initialize re
9 min read