text: Sum of products of all combination taken ( 1 to n ) at a time | Program to find SOP of all combination taken ( 1 to N ) at a time using brute force ; to store sum of every combination ; if we have reached sufficient depth ; find the product of combination ; add the product into sum ; recursion to produce different combination ; function to print sum of products of all combination taken 1 - N at a time ; creating temporary array for storing combination ; call combination with r = i for combination taken i at a time ; displaying sum ; Driver 's code ; storing numbers from 1 - N in array ; calling allCombination
code: #include <iostream> NEW_LINE using namespace std ; int sum = 0 ; void Combination ( int a [ ] , int combi [ ] , int n , int r , int depth , int index ) { if ( index == r ) { int product = 1 ; for ( int i = 0 ; i < r ; i ++ ) product = product * combi [ i ] ; sum += product ; return ; } for ( int i = depth ; i < n ; i ++ ) { combi [ index ] = a [ i ] ; Combination ( a , combi , n , r , i + 1 , index + 1 ) ; } } void allCombination ( int a [ ] , int n ) { for ( int i = 1 ; i <= n ; i ++ ) { int * combi = new int [ i ] ; Combination ( a , combi , n , i , 0 , 0 ) ; cout << " f ( " << i << " ) ▁ - - > ▁ " << sum << " STRNEWLINE " ; sum = 0 ; free ( combi ) ; } } int main ( ) { int n = 5 ; int * a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) a [ i ] = i + 1 ; allCombination ( a , n ) ; return 0 ; }