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 method.
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 result.
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; } |
chevron_right
filter_none
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()); } } |
chevron_right
filter_none
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". |
chevron_right
filter_none
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 |
chevron_right
filter_none
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. ?> |
chevron_right
filter_none
Output :
720
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; } |
chevron_right
filter_none
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)); } } |
chevron_right
filter_none
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". |
chevron_right
filter_none
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. |
chevron_right
filter_none
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. ?> |
chevron_right
filter_none
Output :
720
This article is contributed by Rishabh Jain. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Right most non-zero digit in multiplication of array elements
- Program to print Sum of even and odd elements in an array
- Program to find sum of elements in a given array
- C/C++ Program to find sum of elements in a given array
- Program to print product of even and odd indexed elements in an Array
- Program for scalar multiplication of a matrix
- Program to print multiplication table of a number
- Count divisors of array multiplication
- Replace every array element by multiplication of previous and next
- Find the element whose multiplication with -1 makes array sum 0
- Find remainder of array multiplication divided by n
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Program to print elements of a Matrix row-wise skipping alternate elements
- Find original array from encrypted array (An array of sums of other elements)
- Average of remaining elements after removing K largest and K smallest elements from array



