📜  数组乘积程序

📅  最后修改于: 2022-05-13 01:57:52.264000             🧑  作者: Mango

数组乘积程序

给定一个数组,找出所有数组元素的乘积。
例子 :

Input  : ar[] = {1, 2, 3, 4, 5}
Output : 120
Product of array elements is 1 x 2
x 3 x 4 x 5 = 120.

Input  : ar[] = {1, 6, 3}
Output : 18

C
// C program to find product of array
// elements.
#include 
 
int product(int ar[], int n)
{
    int result = 1;
    for (int i = 0; i < n; i++)
        result = result * ar[i];
    return result;
}
 
// driver code for the above program
int main()
{
    int ar[] = { 1, 2, 3, 4, 5 };
    int n = sizeof(ar) / sizeof(ar[0]);
    printf("%d", product(a, n));
    return 0;
}


Java
// Java program to find product of array
// elements.
class GFG{
 
    static int product(int ar[], int n)
    {
        int result = 1;
        for (int i = 0; i < n; i++)
            result = result * ar[i];
        return result;
    }
      
    // driver code for the above program
    public static void main(String[] args)
    {
        int ar[] = { 1, 2, 3, 4, 5 };
        int n = ar.length;
        System.out.printf("%d", product(ar, n));
    }
}
 
// This code is contributed by Smitha Dinesh Semwal


Python3
# Python3 program to find
# product of array elements.
def product(ar, n):
 
    result = 1
    for i in range(0, n):
        result = result * ar[i]
    return result
 
 
# Driver Code
ar = [ 1, 2, 3, 4, 5 ]
n = len(ar)
 
print(product(ar, n))
 
# This code is contributed by Smitha Dinesh Semwal.


C#
// C# program to find product of array
// elements.
using System;
 
class GFG {
 
    static int product(int []ar, int n)
    {
        int result = 1;
         
        for (int i = 0; i < n; i++)
            result = result * ar[i];
             
        return result;
    }
     
    // driver code for the above program
    public static void Main()
    {
        int []ar = { 1, 2, 3, 4, 5 };
        int n = ar.Length;
         
        Console.WriteLine(product(ar, n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


C
// C code for above program to find product
// under modulo.
#include 
 
const int MOD = 1000000007;
 
int product(int ar[], int n)
{
    int result = 1;
    for (int i = 0; i < n; i++)
        result = (result * ar[i]) % MOD;
    return result;
}
 
// driver code for the above program
int main()
{
    int ar[] = { 1, 2, 3, 4, 5 };
    int n = sizeof(ar) / sizeof(ar[0]);
    printf("%d", product(ar, n));
    return 0;
}


Java
// Java code for above program to find product
// under modulo.
class GFG {
     
    static final int MOD = 1000000007;
 
    static int product(int ar[], int n)
    {
        int result = 1;
        for (int i = 0; i < n; i++)
            result = (result * ar[i]) % MOD;
             
        return result;
    }
 
    // driver code for the above program
    public static void main(String[] args)
    {
        int ar[] = { 1, 2, 3, 4, 5 };
        int n = ar.length;
         
        System.out.printf("%d", product(ar, n));
    }
}
 
// This code is contributed by  Smitha Dinesh Semwal.


Python3
# Python 3 code for above
# program to find product
# under modulo.
 
MOD = 1000000007
 
def product(ar, n):
 
    result = 1
    for i in range(0, n):
        result = (result * ar[i]) % MOD
    return result
 
 
# driver code for the
# above program
ar = [1, 2, 3, 4, 5]
n = len(ar)
 
print(product(ar, n))
 
# This code is contributed by
# Smitha Dinesh Semwal


C#
// C# code for above program to find product
// under modulo.
using System;
class GFG {
     
    static  int MOD = 1000000007;
 
    static int product(int []ar, int n)
    {
        int result = 1;
        for (int i = 0; i < n; i++)
            result = (result * ar[i]) % MOD;
             
        return result;
    }
 
    // driver code for the above program
    public static void Main()
    {
        int []ar = { 1, 2, 3, 4, 5 };
        int n = ar.Length;
         
        Console.WriteLine(product(ar, n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出 :

120

上面的代码可能会导致溢出。因此,总是希望在模下计算乘积。其工作的原因是模的简单分配性质。

( a * b) % c = ( ( a % c ) * ( b % c ) ) % c

下面是一个程序,用于查找并打印此 Modulo (10^9 +7) 数组中所有数字的乘积。

C

// C code for above program to find product
// under modulo.
#include 
 
const int MOD = 1000000007;
 
int product(int ar[], int n)
{
    int result = 1;
    for (int i = 0; i < n; i++)
        result = (result * ar[i]) % MOD;
    return result;
}
 
// driver code for the above program
int main()
{
    int ar[] = { 1, 2, 3, 4, 5 };
    int n = sizeof(ar) / sizeof(ar[0]);
    printf("%d", product(ar, n));
    return 0;
}

Java

// Java code for above program to find product
// under modulo.
class GFG {
     
    static final int MOD = 1000000007;
 
    static int product(int ar[], int n)
    {
        int result = 1;
        for (int i = 0; i < n; i++)
            result = (result * ar[i]) % MOD;
             
        return result;
    }
 
    // driver code for the above program
    public static void main(String[] args)
    {
        int ar[] = { 1, 2, 3, 4, 5 };
        int n = ar.length;
         
        System.out.printf("%d", product(ar, n));
    }
}
 
// This code is contributed by  Smitha Dinesh Semwal.

Python3

# Python 3 code for above
# program to find product
# under modulo.
 
MOD = 1000000007
 
def product(ar, n):
 
    result = 1
    for i in range(0, n):
        result = (result * ar[i]) % MOD
    return result
 
 
# driver code for the
# above program
ar = [1, 2, 3, 4, 5]
n = len(ar)
 
print(product(ar, n))
 
# This code is contributed by
# Smitha Dinesh Semwal

C#

// C# code for above program to find product
// under modulo.
using System;
class GFG {
     
    static  int MOD = 1000000007;
 
    static int product(int []ar, int n)
    {
        int result = 1;
        for (int i = 0; i < n; i++)
            result = (result * ar[i]) % MOD;
             
        return result;
    }
 
    // driver code for the above program
    public static void Main()
    {
        int []ar = { 1, 2, 3, 4, 5 };
        int n = ar.Length;
         
        Console.WriteLine(product(ar, n));
    }
}
 
// This code is contributed by vt_m.

PHP


Javascript


输出 :

120