Find sum of factorials in an array | C ++ implementation of the approach ; Function to return the factorial of n ; Function to return the sum of factorials of the array elements ; To store the required sum ; Add factorial of all the elements ; Driver code
#include <iostream> NEW_LINE #include <bits/stdc++.h> NEW_LINE using namespace std ; int factorial ( int n ) { int f = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { f *= i ; } return f ; } int sumFactorial ( int * arr , int n ) { int s = 0 , i ; for ( i = 0 ; i < n ; i ++ ) { s += factorial ( arr [ i ] ) ; } return s ; } int main ( ) { int arr [ ] = { 7 , 3 , 5 , 4 , 8 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << sumFactorial ( arr , n ) ; return 0 ; }