Find sum of factorials in an array - GeeksforGeeks

15 Apr, 2021
Given an array arr[] of N integers. The task is to find the sum of factorials of each element of the array.
Examples: 
Input: arr[] = {7, 3, 5, 4, 8} Output: 45510 7! + 3! + 5! + 4! + 8! = 5040 + 6 + 120 + 24 + 40320 = 45510
Input: arr[] = {2, 1, 3} Output: 9  
Approach: Implement a function factorial(n) that finds the factorial of n and initialize sum = 0. Now, traverse the given array and for each element arr[i] update sum = sum + factorial(arr[i]). Print the calculated sum in the end.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the approach#include <iostream>#include <bits/stdc++.h>using namespace std; // Function to return the factorial of nint factorial(int n){    int f = 1;    for (int i = 1; i <= n; i++)    {        f *= i;    }    return f;} // Function to return the sum of// factorials of the array elementsint sumFactorial(int *arr, int n){     // To store the required sum    int s = 0,i;    for (i = 0; i < n; i++)    {         // Add factorial of all the elements        s += factorial(arr[i]);    }    return s;} // Driver codeint main(){    int arr[] = { 7, 3, 5, 4, 8 };    int n = sizeof(arr) / sizeof(arr[0]);    cout << sumFactorial(arr, n);    return 0;}     // This code is contributed by 29AjayKumar
// Java implementation of the approachclass GFG {     // Function to return the factorial of n    static int factorial(int n)    {        int f = 1;        for (int i = 1; i <= n; i++) {            f *= i;        }        return f;    }     // Function to return the sum of    // factorials of the array elements    static int sumFactorial(int[] arr, int n)    {         // To store the required sum        int s = 0;        for (int i = 0; i < n; i++) {             // Add factorial of all the elements            s += factorial(arr[i]);        }        return s;    }     // Driver Code    public static void main(String[] args)    {        int[] arr = { 7, 3, 5, 4, 8 };        int n = arr.length;        System.out.println(sumFactorial(arr, n));    }}
# Python implementation of the approach # Function to return the factorial of ndef factorial(n):    f = 1;    for i in range(1, n + 1):        f *= i;    return f; # Function to return the sum of# factorials of the array elementsdef sumFactorial(arr, n):     # To store the required sum    s = 0;    for i in range(0,n):         # Add factorial of all the elements        s += factorial(arr[i]);    return s; # Driver codearr = [7, 3, 5, 4, 8 ];n = len(arr);print(sumFactorial(arr, n)); # This code contributed by Rajput-Ji
// C# implementation of the approachusing System; class GFG{     // Function to return the factorial of n    static int factorial(int n)    {        int f = 1;        for (int i = 1; i <= n; i++)        {            f *= i;        }        return f;    }     // Function to return the sum of    // factorials of the array elements    static int sumFactorial(int[] arr, int n)    {         // To store the required sum        int s = 0;        for (int i = 0; i < n; i++)        {             // Add factorial of all the elements            s += factorial(arr[i]);        }        return s;    }     // Driver Code    public static void Main()    {        int[] arr = { 7, 3, 5, 4, 8 };        int n = arr.Length;        Console.WriteLine(sumFactorial(arr, n));    }} // This code is contributed by Ryuga
<?php // PHP implementation of the approach // Function to return the factorial of nfunction factorial( $n){    $f = 1;    for ( $i = 1; $i <= $n; $i++)    {        $f *=$i;    }    return $f;} // Function to return the sum of// factorials of the array elementsfunction sumFactorial($arr,  $n){     // To store the required sum    $s = 0;    for ($i = 0; $i < $n; $i++)    {         // Add factorial of all the elements        $s += factorial($arr[$i]);    }    return $s;} // Driver code $arr = array( 7, 3, 5, 4, 8 );$n = sizeof($arr);echo sumFactorial($arr, $n); // This code is contributed by ihritik ?>    
<script> // Javascript implementation of the approach // Function to return the factorial of nfunction factorial(n){    let f = 1;    for(let i = 1; i <= n; i++)    {        f *= i;    }    return f;} // Function to return the sum of// factorials of the array elementsfunction sumFactorial(arr, n){     // To store the required sum    let s = 0;    for (let i = 0; i < n; i++)    {                 // Add factorial of all the elements        s += factorial(arr[i]);    }    return s;} // Driver codelet arr = [ 7, 3, 5, 4, 8 ];let n = arr.length; document.write(sumFactorial(arr, n)); // This code is contributed by bobby </script>
45510
 
ankthon
29AjayKumar
Rajput-Ji
ihritik
gottumukkalabobby
factorial
Arrays
Mathematical
School Programming
Arrays
Mathematical
factorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Stack Data Structure (Introduction and Program)
Top 50 Array Coding Problems for Interviews
Introduction to Arrays
Multidimensional Arrays in Java
Linear Search
Program for Fibonacci numbers
C++ Data Types
Write a program to print all permutations of a given string
Set in C++ Standard Template Library (STL)
Program to find GCD or HCF of two numbers