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++
#include<bits/stdc++.h>
using namespace std;
int multiply( int array[], int n)
{
int pro = 1;
for ( int i = 0; i < n; i++)
pro = pro * array[i];
return pro;
}
int main()
{
int array[] = {1, 2, 3, 4, 5, 6};
int n = sizeof (array) / sizeof (array[0]);
cout << multiply(array, n);
return 0;
}
|
Java
import java.io.*;
public class GFG
{
static int arr[] = { 1 , 2 , 3 , 4 , 5 , 6 };
static int multiply()
{
int pro = 1 ;
for ( int i = 0 ; i < arr.length; i++)
pro = pro * arr[i];
return pro;
}
public static void main(String[] args)
{
System.out.println(multiply());
}
}
|
Python3
def multiply( array , n ):
pro = 1
for i in range (n):
pro = pro * array[i]
return pro
array = [ 1 , 2 , 3 , 4 , 5 , 6 ]
n = len (array)
print (multiply(array, n))
|
C#
using System;
class GFG
{
static int []arr = {1, 2, 3, 4, 5, 6};
static int multiply()
{
int pro = 1;
for ( int i = 0; i < arr.Length; i++)
pro = pro * arr[i];
return pro;
}
public static void Main()
{
Console.Write(multiply());
}
}
|
PHP
<?php
function multiply( $arr , $n )
{
$pro = 1;
for ( $i = 0; $i < $n ; $i ++)
$pro = $pro * $arr [ $i ];
return $pro ;
}
$arr = array (1, 2, 3, 4, 5, 6);
$n = sizeof( $arr ) / sizeof( $arr [0]);
echo multiply( $arr , $n );
return 0;
?>
|
Javascript
<script>
var arr = [ 1, 2, 3, 4, 5, 6 ];
function multiply() {
var pro = 1;
for (i = 0; i < arr.length; i++)
pro = pro * arr[i];
return pro;
}
document.write(multiply());
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Recursive Method:
C++
#include<iostream>
using namespace std;
int multiply( int a[], int n)
{
if (n == 0)
return (a[n]);
else
return (a[n] * multiply(a, n - 1));
}
int main()
{
int array[] = {1, 2, 3, 4, 5, 6};
int n = sizeof (array) / sizeof (array[0]);
cout << multiply(array, n - 1)
<< endl;
return 0;
}
|
Java
import java.io.*;
public class GFG
{
static int arr[] = { 1 , 2 , 3 , 4 , 5 , 6 };
static int multiply( int a[], int n)
{
if (n == 0 )
return (a[n]);
else
return (a[n] * multiply(a, n - 1 ));
}
public static void main(String[] args)
{
System.out.println(multiply(arr,
arr.length - 1 ));
}
}
|
Python3
def multiply( a , n ):
if n = = 0 :
return (a[n])
else :
return (a[n] * multiply(a, n - 1 ))
array = [ 1 , 2 , 3 , 4 , 5 , 6 ]
n = len (array)
print (multiply(array, n - 1 ))
|
C#
using System;
class GFG
{
static int []arr = {1, 2, 3, 4, 5, 6};
static int multiply( int []a, int n)
{
if (n == 0)
return (a[n]);
else
return (a[n] * multiply(a, n - 1));
}
public static void Main()
{
Console.Write(multiply(arr,
arr.Length - 1));
}
}
|
PHP
<?php
function multiply( $a , $n )
{
if ( $n == 0)
return ( $a [ $n ]);
else
return ( $a [ $n ] *
multiply( $a , $n - 1));
}
$array = array (1, 2, 3, 4, 5, 6);
$n = count ( $array );
echo multiply( $array , $n - 1)
?>
|
Javascript
<script>
var arr = [ 1, 2, 3, 4, 5, 6 ];
function multiply(a , n) {
if (n == 0)
return (a[n]);
else
return (a[n] * multiply(a, n - 1));
}
document.write(multiply(arr,
arr.length - 1));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Using Library functions:
C++
#include <iostream>
#include <numeric>
using namespace std;
int multiply( int array[], int n)
{
int pro = 1;
return accumulate(array, array + n, pro,
multiplies< int >());
}
int main()
{
int array[] = { 1, 2, 3, 4, 5, 6 };
int n = sizeof (array) / sizeof (array[0]);
cout << multiply(array, n);
return 0;
}
|
Java
import java.util.Arrays;
import java.util.function.IntBinaryOperator;
public class GFG {
public static int multiply( int [] array)
{
int pro = 1 ;
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 };
int n = array.length;
System.out.println(multiply(array));
}
}
|
Python3
from functools import reduce
def multiply(array, n):
return reduce (( lambda x, y: x * y), array)
array = [ 1 , 2 , 3 , 4 , 5 , 6 ]
n = len (array)
print (multiply(array, n))
|
C#
using System;
using System.Linq;
public class GFG {
static int Multiply( int [] array, int n)
{
int pro = 1;
return array.Aggregate(pro, (current, t) =
> current * t);
}
static public void Main( string [] args)
{
int [] array = { 1, 2, 3, 4, 5, 6 };
int n = array.Length;
Console.WriteLine(Multiply(array, n));
}
}
|
Javascript
<script>
function multiply(array) {
let pro = 1;
return array.reduce((acc, cur) => acc * cur, pro);
}
let array = [1, 2, 3, 4, 5, 6];
let n = array.length;
console.log(multiply(array));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...