Program for multiplication of array elements
Last Updated :
01 Mar, 2023
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 : 720
Here, product of elements = 1*2*3*4*5*6 = 720
Input : array[] = {1, 3, 5, 7, 9}
Output : 945
Iterative Method: We initialize result as 1. We traverse array from left to right and multiply elements with results.
Implementation:
C++
// Iterative C++ program to
// multiply array elements
#include<bits/stdc++.h>
using namespace std;
// Function to calculate the
// product of the array
int multiply(int array[], int n)
{
int pro = 1;
for (int i = 0; i < n; i++)
pro = pro * array[i];
return pro;
}
// Driver Code
int main()
{
int array[] = {1, 2, 3, 4, 5, 6};
int n = sizeof(array) / sizeof(array[0]);
// Function call to calculate product
cout << multiply(array, n);
return 0;
}
Java
// Iterative Java program to
// multiply array elements
import java.io.*;
public class GFG
{
static int arr[] = {1, 2, 3, 4, 5, 6};
// Method to calculate the
// product of the array
static int multiply()
{
int pro = 1;
for (int i = 0; i < arr.length; i++)
pro = pro * arr[i];
return pro;
}
// Driver Code
public static void main(String[] args)
{
// Method call to calculate product
System.out.println(multiply());
}
}
Python3
# Iterative Python3 code to
# multiply list elements
# Function to calculate
# the product of the array
def multiply( array , n ):
pro = 1
for i in range(n):
pro = pro * array[i]
return pro
# Driver code
array = [1, 2, 3, 4, 5, 6]
n = len(array)
# Function call to
# calculate product
print(multiply(array, n))
# This code is contributed
# by "Sharad_Bhardwaj".
C#
// Iterative C# program to
// multiply array elements
using System;
class GFG
{
static int []arr = {1, 2, 3, 4, 5, 6};
// Method to calculate the
// product of the array
static int multiply()
{
int pro = 1;
for (int i = 0; i < arr.Length; i++)
pro = pro * arr[i];
return pro;
}
// Driver Code
public static void Main()
{
// Method call to calculate product
Console.Write(multiply());
}
}
// This code is contributed by nitin mittal
PHP
<?php
// Iterative PHP program to
// multiply array elements
// Function to calculate the
// product of the array
function multiply($arr, $n)
{
$pro = 1;
for ($i = 0; $i < $n; $i++)
$pro = $pro * $arr[$i];
return $pro;
}
// Driver Code
$arr = array(1, 2, 3, 4, 5, 6);
$n = sizeof($arr) / sizeof($arr[0]);
// Function call to
// calculate product
echo multiply($arr, $n);
return 0;
// This code is contributed by nitin mittal.
?>
JavaScript
<script>
// Iterative javascript program to
// multiply array elements
var arr = [ 1, 2, 3, 4, 5, 6 ];
// Method to calculate the
// product of the array
function multiply() {
var pro = 1;
for (i = 0; i < arr.length; i++)
pro = pro * arr[i];
return pro;
}
// Driver Code
// Method call to calculate product
document.write(multiply());
// This code contributed by aashish1995
</script>
Time Complexity: O(n)
Auxiliary Space: O(1)
Recursive Method:
C++
// Recursive C++ program to
// multiply array elements
#include<iostream>
using namespace std;
// Function to calculate the
// product of array using recursion
int multiply(int a[], int n)
{
// Termination condition
if (n == 0)
return(a[n]);
else
return (a[n] * multiply(a, n - 1));
}
// Driver Code
int main()
{
int array[] = {1, 2, 3, 4, 5, 6};
int n = sizeof(array) / sizeof(array[0]);
// Function call to
// calculate the product
cout << multiply(array, n - 1)
<< endl;
return 0;
}
Java
// Recursive Java program to
// multiply array elements
import java.io.*;
public class GFG
{
static int arr[] = {1, 2, 3, 4, 5, 6};
// Method to calculate the product
// of the array using recursion
static int multiply(int a[], int n)
{
// Termination condition
if (n == 0)
return(a[n]);
else
return (a[n] * multiply(a, n - 1));
}
// Driver Code
public static void main(String[] args)
{
// Method call to
// calculate product
System.out.println(multiply(arr,
arr.length - 1));
}
}
Python3
# Recursive Python3 code
# to multiply array elements
# Function to calculate the product
# of array using recursion
def multiply( a , n ):
# Termination condition
if n == 0:
return(a[n])
else:
return (a[n] * multiply(a, n - 1))
# Driver Code
array = [1, 2, 3, 4, 5, 6]
n = len(array)
# Function call to
# calculate the product
print(multiply(array, n - 1))
# This code is contributed
# by "Sharad_Bhardwaj".
C#
// Recursive C# program to
// multiply array elements
using System;
class GFG
{
static int []arr = {1, 2, 3, 4, 5, 6};
// Method to calculate the product
// of the array using recursion
static int multiply(int []a, int n)
{
// Termination condition
if (n == 0)
return(a[n]);
else
return (a[n] * multiply(a, n - 1));
}
// Driver Code
public static void Main()
{
// Method call to
// calculate product
Console.Write(multiply(arr,
arr.Length - 1));
}
}
// This code is contributed by Nitin Mittal.
PHP
<?php
// Recursive PHP program to
// multiply array elements
// Function to calculate the
// product of array using recursion
function multiply( $a, $n)
{
// Termination condition
if ($n == 0)
return($a[$n]);
else
return ($a[$n] *
multiply($a, $n - 1));
}
// Driver Code
$array = array(1, 2, 3, 4, 5, 6);
$n = count($array);
// Function call to
// calculate the product
echo multiply($array, $n - 1)
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// Recursive javascript program to
// multiply array elements
var arr = [ 1, 2, 3, 4, 5, 6 ];
// Method to calculate the product
// of the array using recursion
function multiply(a , n) {
// Termination condition
if (n == 0)
return (a[n]);
else
return (a[n] * multiply(a, n - 1));
}
// Driver Code
// Method call to
// calculate product
document.write(multiply(arr,
arr.length - 1));
// This code is contributed by todaysgaurav
</script>
Time Complexity: O(n)
Auxiliary Space: O(n)
Using Library functions:
C++
// C++ program for multiplication of array elements
#include <iostream>
/*In C++, we can quickly find array product using
accumulate() and multiplies<>() defined in numeric library*/
#include <numeric>
using namespace std;
// Function to calculate the
// product of the array
int multiply(int array[], int n)
{
// The pro specifies the initial value to be considered
int pro = 1;
/*
Here accumulate() take 4 parameters:
begening of array, end of array, the initial value
and the binary operation function object that will be
applied
*/
return accumulate(array, array + n, pro,
multiplies<int>());
}
int main()
{
int array[] = { 1, 2, 3, 4, 5, 6 };
// get length of array
int n = sizeof(array) / sizeof(array[0]);
cout << multiply(array, n);
return 0;
// This code is contributed by Shivesh Kumar Dwivedi
}
Java
// Java program for multiplication of array elements
import java.util.Arrays;
import java.util.function.IntBinaryOperator;
public class GFG {
// Function to calculate the product of the array
public static int multiply(int[] array)
{
// The pro specifies the initial value to be
// considered
int pro = 1;
/*
Here Arrays.stream() method is used to get an
IntStream of array, then reduce() method is used
with the help of IntBinaryOperator interface and
multiplies operation to perform the binary
operation between the array elements and the
initial value.
*/
return Arrays.stream(array).reduce(
pro, new IntBinaryOperator() {
@Override
public int applyAsInt(int left, int right)
{
return left * right;
}
});
}
public static void main(String[] args)
{
int[] array = { 1, 2, 3, 4, 5, 6 };
// get length of array
int n = array.length;
System.out.println(multiply(array));
}
}
// this code is contributed by bhardwajji
Python3
# python3 program for multiplication of array elements
# In python3, we can quickly find array product using
# reduce available in functools library
from functools import reduce
# Function to calculate the
# product of the array
def multiply(array, n):
# The reduce() only takes the name of the array/list as a parameter
return reduce((lambda x, y: x * y), array)
array = [1, 2, 3, 4, 5, 6]
# get length of array
n = len(array)
print(multiply(array, n))
# This code is contributed by Abhijeet Kumar(abhijeet19403)
C#
// C# program for multiplication of array elements
using System;
// In C#, we can quickly find array
// product using using the Aggregate
// method from the System.Linq namespace
using System.Linq;
public class GFG {
// Function to calculate the product of the array
static int Multiply(int[] array, int n)
{
// The pro specifies the initial value to be
// considered
int pro = 1;
// here Aggregate method takes two arguments
// an initial value (in this case, pro) and a
// delegate function that defines how to
// aggregate the values in the array
return array.Aggregate(pro, (current, t) =
> current * t);
}
// Driver Code
static public void Main(string[] args)
{
int[] array = { 1, 2, 3, 4, 5, 6 };
// get length of array
int n = array.Length;
Console.WriteLine(Multiply(array, n));
}
}
// This code is contributed by Prasad Kandekar(prasad264)
JavaScript
<script>
// Function to calculate the product of the array
function multiply(array) {
// The pro specifies the initial value to be considered
let pro = 1;
/*
Here the reduce() method is used with the help of an arrow function to perform the binary operation between
the array elements and the initial value.
*/
return array.reduce((acc, cur) => acc * cur, pro);
}
let array = [1, 2, 3, 4, 5, 6];
// get length of array
let n = array.length;
console.log(multiply(array));
</script>
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Program for scalar multiplication of a matrix Given a 2D matrix mat[][] with n rows and m columns and a scalar element k, the task is to find out the scalar product of the given matrix.Examples: Input: mat[][] = [[2, 3], [5, 4]]k = 5Output: [[10, 15], [25, 20]]Input:mat[][] = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]k = 4Output: [[4, 8, 12], [16, 20, 2
4 min read
Replace every array element by multiplication of previous and next Given an array of integers, update every element with the multiplication of previous and next elements with the following exceptions. a) The First element is replaced by the multiplication of the first and second. b) The last element is replaced by multiplication of the last and second last. Example
7 min read
Count divisors of array multiplication Given an array with N elements, the task is to find the count of factors of a number X, which is the product of all array elements. Examples: Input : 5 5 Output : 3 5 * 5 = 25, the factors of 25 are 1, 5, 25 whose count is 3 Input : 3 5 7 Output : 8 3 * 5 * 7 = 105, the factors of 105 are 1, 3, 5, 7
13 min read
Bitwise multiplication of Binary Array with K Given an array nums[] representing a binary number and integer K, the task is to perform bitwise multiplication by a given integer K (where K is a power of 2). Return a new array representing the product. Examples: Input: nums[] = {1, 0, 1}, K = 8 Output: {1, 0, 1, 0, 0, 0} Explanation: The binary n
6 min read
Find remainder of array multiplication divided by n Given multiple numbers and a number n, the task is to print the remainder after multiply all the number divide by n. Examples: Input : arr[] = {100, 10, 5, 25, 35, 14}, n = 11 Output : 9 100 x 10 x 5 x 25 x 35 x 14 = 61250000 % 11 = 9 Input : arr[] = {100, 10}, n = 5 Output : 0 100 x 10 = 1000 % 5 =
7 min read