Open In App

Program for product of array

Improve
Improve
Like Article
Like
Save
Share
Report

Given an array, find a 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

Implementation: 

Two Pointer Approach:

Approach:
1) Input: arr[]
2) Initialize with start and last pointers i.e i,j. and also initialize product=0
3) Iterate i=0 to i>j;
    i+=1
    j-=1
4) Multiply first and last numbers at a time while iterating.
5) if i==j multiply element only once.

C++




#include <iostream>
using namespace std;
  
int main()
{
  
    // array Elements
    int arr[] = { 1, 2, 3, 4, 5 };
    int product = 1;
  
    // initialize start and last pointers
    int i = 0;
    int j = sizeof(arr) / sizeof(arr[0]) - 1;
  
    // add first and last simultaneously
    while (i < j) {
        product *= arr[i] * arr[j];
        i++;
        j--;
    }
  
    // multiply only one element
    if (i == j) {
        product *= arr[i];
    }
  
    // printing product
    cout << product << endl;
}
  
// This code is contributed by Potta Lokesh


Java




/*package whatever //do not write package name here */
import java.io.*;
  
class GFG {
  public static void main (String[] args)
  {
      
    // array Elements
    int arr[] = { 1, 2, 3, 4, 5 };
    int product = 1;
  
    // initialize start and last pointers
    int i = 0;
    int j = arr.length- 1;
  
    // add first and last simultaneously
    while (i < j) {
      product *= arr[i] * arr[j];
      i++;
      j--;
    }
  
    // multiply only one element
    if (i == j) {
      product *= arr[i];
    }
  
    // printing product
    System.out.println( product);
  }
}
  
// This code is contributed by pradeepkumarppk2003


Python3




# Python code to product elements in array
  
#array Elements
arr=[1,2,3,4,5]
product=1
  
#initialize start and last pointers
i=0
j=len(arr)-1
  
#add first and last simultaneously 
while(i<j):
    product*=arr[i]*arr[j]
    i+=1
    j-=1
      
#multiply only one element
if(i==j):
    product*=arr[i]
  
#printing product
print(product)


C#




// C# code to product elements in array
using System;
  
public class GFG {
    public static void Main(string[] args)
    {
  
        // array Elements
        int[] arr = { 1, 2, 3, 4, 5 };
        int product = 1;
  
        // initialize start and last pointers
        int i = 0;
        int j = arr.Length - 1;
  
        // add first and last simultaneously
        while (i < j) {
            product *= arr[i] * arr[j];
            i += 1;
            j -= 1;
        }
        // multiply only one element
        if (i == j)
            product *= arr[i];
  
        // printing product
        Console.WriteLine(product);
    }
}
  
// This code is contributed by Karandeep1234


Javascript




// Javascript code to product elements in array
  
// array Elements
let arr = [1,2,3,4,5];
let product = 1;
  
// initialize start and last pointers
let i = 0;
let j = arr.length-1;
  
// add first and last simultaneously 
while(i < j)
{
    product *= arr[i]*arr[j];
    i += 1;
    j -= 1;
}
      
// multiply only one element
if(i == j)
    product *= arr[i];
  
// printing product
console.log(product);
  
// This code is contributed by poojaagarwal2.


Output

120

Time Complexity : O(n)
Auxiliary Space : O(1) 

Implementation:

C++




// C++ program to find product of array elements.
  
#include <iostream>
using namespace std;
  
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]);
    cout << product(ar, n);
    return 0;
}
  
// This code is contributed by lokeshmvs21.


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.
import java.io.*;
public 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>


Output

120

Time Complexity : O(n)
Auxiliary Space : O(1) 

The above code may cause overflow. Therefore, it is always desired to compute product under modulo. The reason for its working is the simple distributive property of modulo. 

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

Below is a program to find and print the product of all the number in this array of Modulo (10^9 +7)

Implementation:

C++




// C++ code for above program to find product
// under modulo.
  
#include <iostream>
using namespace std;
  
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]);
    cout << product(ar, n);
    return 0;
}
  
// This code is contributed by lokeshmvs21.


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.
import java.io.*;
public 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>


Output

120

Time Complexity : O(n)
Auxiliary Space : O(1) 

 



Last Updated : 17 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads