A Product Array Puzzle |  ; Function to print product array for a given array arr [ ] of size n ; Base case ; Allocate memory for the product array ; Initialize the product array as 1 ; In this loop , temp variable contains product of elements on left side excluding arr [ i ] ; Initialize temp to 1 for product on right side ; In this loop , temp variable contains product of elements on right side excluding arr [ i ] ; print the constructed prod array ; Driver program to test above functions
using System ; class GFG { static void productArray ( int [ ] arr , int n ) { if ( n == 1 ) { Console . Write ( 0 ) ; return ; } int i , temp = 1 ; int [ ] prod = new int [ n ] ; for ( int j = 0 ; j < n ; j ++ ) prod [ j ] = 1 ; for ( i = 0 ; i < n ; i ++ ) { prod [ i ] = temp ; temp *= arr [ i ] ; } temp = 1 ; for ( i = n - 1 ; i >= 0 ; i -- ) { prod [ i ] *= temp ; temp *= arr [ i ] ; } for ( i = 0 ; i < n ; i ++ ) Console . Write ( prod [ i ] + " ▁ " ) ; return ; } public static void Main ( ) { int [ ] arr = { 10 , 3 , 5 , 6 , 2 } ; int n = arr . Length ; Console . WriteLine ( " The ▁ product ▁ array ▁ is ▁ : ▁ " ) ; productArray ( arr , n ) ; } }