Given an array, find product of all array elements.
Examples :
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
#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;
}
int main()
{
int ar[] = { 1, 2, 3, 4, 5 };
int n = sizeof (ar) / sizeof (ar[0]);
printf ( "%d" , product(a, n));
return 0;
}
|
Java
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;
}
public static void main(String[] args)
{
int ar[] = { 1 , 2 , 3 , 4 , 5 };
int n = ar.length;
System.out.printf( "%d" , product(ar, n));
}
}
|
Python3
def product(ar, n):
result = 1
for i in range ( 0 , n):
result = result * ar[i]
return result
ar = [ 1 , 2 , 3 , 4 , 5 ]
n = len (ar)
print (product(ar, n))
|
C#
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;
}
public static void Main()
{
int []ar = { 1, 2, 3, 4, 5 };
int n = ar.Length;
Console.WriteLine(product(ar, n));
}
}
|
PHP
<?php
function product( $ar , $n )
{
$result = 1;
for ( $i = 0; $i < $n ; $i ++)
$result = $result * $ar [ $i ];
return $result ;
}
$ar = array ( 1, 2, 3, 4, 5 );
$n = count ( $ar );
print ((int)product( $ar , $n ));
?>
|
Output :
120
The above code may cause overflow. Therefore it is always desired to compute product under modulo. The reason of its working is simple distributive property of modulo.
( a * b) % c = ( ( a % c ) * ( b % c ) ) % c
Below is program to find to find and print the product of all the number in this array of Modulo (10^9 +7).
C
#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;
}
int main()
{
int ar[] = { 1, 2, 3, 4, 5 };
int n = sizeof (ar) / sizeof (ar[0]);
printf ( "%d" , product(ar, n));
return 0;
}
|
Java
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;
}
public static void main(String[] args)
{
int ar[] = { 1 , 2 , 3 , 4 , 5 };
int n = ar.length;
System.out.printf( "%d" , product(ar, n));
}
}
|
Python3
MOD = 1000000007
def product(ar, n):
result = 1
for i in range ( 0 , n):
result = (result * ar[i]) % MOD
return result
ar = [ 1 , 2 , 3 , 4 , 5 ]
n = len (ar)
print (product(ar, n))
|
C#
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;
}
public static void Main()
{
int []ar = { 1, 2, 3, 4, 5 };
int n = ar.Length;
Console.WriteLine(product(ar, n));
}
}
|
PHP
<?php
function product( $ar , $n )
{
$result = 1;
for ( $i = 0; $i < $n ; $i ++)
$result = ( $result *
$ar [ $i ]) % 1000000007;
return $result ;
}
$ar = array ( 1, 2, 3, 4, 5 );
$n = count ( $ar );
print (product( $ar , $n ));
?>
|
Output :
120
This article is contributed by Shivani Baghel. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.