算法:如何计算数组元素乘积?(使用迭代和递归)

给定一个数组, 我们必须使用迭代和递归方法来计算数组的乘积。

例子 :

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

 

迭代方法:

我们将结果初始化为1。我们从左到右遍历数组, 并将元素与结果相乘。

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
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.
?>

输出:

720

 

递归方法:

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
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. 
?>

 

输出:

720

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

更多数据结构和算法相关内容请参考:lsbin - IT开发技术https://www.lsbin.com/

参考更多算法和数据结构内容:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值