Dada una array, encuentre un producto de todos los elementos de la array.
Ejemplos:
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
Implementación:
C
// C program to find product of array // elements. #include <stdio.h> 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(ar, 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
<?php // PHP program to find product // of array elements. function product($ar, $n) { $result = 1; for ($i = 0; $i < $n; $i++) $result = $result * $ar[$i]; return $result; } // Driver Code $ar = array( 1, 2, 3, 4, 5 ); $n = count($ar); print((int)product($ar, $n)); // This code is contributed by Sam007 ?>
Javascript
<script> // Javascript program to find product of array // elements. function product(ar,n) { let result = 1; for (let i = 0; i < n; i++) result = result * ar[i]; return result; } // driver code for the above program let ar = [ 1, 2, 3, 4, 5 ]; let n = ar.length; document.write(parseInt(product(ar, n))); // This code is Contributed by sravan kumar </script>
120
Tiempo Complejidad : O(n)
Espacio Auxiliar : O(1)
El código anterior puede causar desbordamiento. Por lo tanto, siempre se desea calcular el producto bajo módulo. La razón de su funcionamiento es la propiedad distributiva simple del módulo.
( a * b) % c = ( ( a % c ) * ( b % c ) ) % c
A continuación se muestra un programa para encontrar e imprimir el producto de todos los números en esta array de Modulo (10^9 +7) .
Implementación:
C
// C code for above program to find product // under modulo. #include <stdio.h> 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
<?php // PHP code for above program // to find product under modulo. function product($ar, $n) { $result = 1; for ($i = 0; $i < $n; $i++) $result = ($result * $ar[$i]) % 1000000007; return $result; } // Driver Code $ar = array( 1, 2, 3, 4, 5 ); $n = count($ar); print(product($ar, $n)); // This code is contributed by Sam007 ?>
Javascript
<script> // Javascript code for above program to find product under modulo. let MOD = 1000000007; function product(ar, n) { let result = 1; for (let i = 0; i < n; i++) result = (result * ar[i]) % MOD; return result; } let ar = [ 1, 2, 3, 4, 5 ]; let n = ar.length; document.write(product(ar, n)); </script>
120
Tiempo Complejidad : O(n)
Espacio Auxiliar : O(1)
Este artículo es una contribución de Shivani Baghel . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA