# Program for multiplication of array elements

• Difficulty Level : Easy
• Last Updated : 20 Oct, 2022

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` `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``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

 ``

## Javascript

 ``

Output

`720`

Time Complexity: O(n)
Auxiliary Space: O(1)

Recursive Method:

## C++

 `// Recursive C++ program to``// multiply array elements``#include` `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``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

 ``

## Javascript

 ``

Output

`720`

Time Complexity: O(n)
Auxiliary Space: O(n)

Using C++ STL:

## C++

 `// C++ program for multiplication of array elements``#include ``/*In C++, we can quickly find array product using``accumulate() and multiplies<>() defined in numeric library*/``#include ` `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``}`

Output

`720`

Time Complexity: O(n)
Auxiliary Space: O(1)

This article is contributed by Rishabh Jain. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

My Personal Notes arrow_drop_up