# Check if a given number divides the sum of the factorials of its digits

Given an integer N, the task is to check whether N divides the sum of the factorials of its digits.

Examples:

Input: N = 19
Output: Yes
1! + 9! = 1 + 362880 = 362881 which is divisible by 19.

Input: N = 20
Output: No
0! + 2! = 1 + 4 = 5 which is not divisible by 20.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: First, store the factorials of all the digits from 0 to 9 in an array. And, for the given number N check if it divides the sum of the factorials of its digits.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function that returns true if n divides ` `// the sum of the factorials of its digits ` `bool` `isPossible(``int` `n) ` `{ ` ` `  `    ``// To store factorials of digits ` `    ``int` `fac; ` `    ``fac = fac = 1; ` ` `  `    ``for` `(``int` `i = 2; i < 10; i++) ` `        ``fac[i] = fac[i - 1] * i; ` ` `  `    ``// To store sum of the factorials ` `    ``// of the digits ` `    ``int` `sum = 0; ` ` `  `    ``// Store copy of the given number ` `    ``int` `x = n; ` ` `  `    ``// Store sum of the factorials ` `    ``// of the digits ` `    ``while` `(x) { ` `        ``sum += fac[x % 10]; ` `        ``x /= 10; ` `    ``} ` ` `  `    ``// If it is divisible ` `    ``if` `(sum % n == 0) ` `        ``return` `true``; ` ` `  `    ``return` `false``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 19; ` ` `  `    ``if` `(isPossible(n)) ` `        ``cout << ``"Yes"``; ` `    ``else` `        ``cout << ``"No"``; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach  ` `class` `GFG  ` `{ ` `     `  `    ``// Function that returns true if n divides  ` `    ``// the sum of the factorials of its digits  ` `    ``static` `boolean` `isPossible(``int` `n)  ` `    ``{  ` `     `  `        ``// To store factorials of digits  ` `        ``int` `fac[] = ``new` `int``[``10``];  ` `        ``fac[``0``] = fac[``1``] = ``1``;  ` `     `  `        ``for` `(``int` `i = ``2``; i < ``10``; i++)  ` `            ``fac[i] = fac[i - ``1``] * i;  ` `     `  `        ``// To store sum of the factorials  ` `        ``// of the digits  ` `        ``int` `sum = ``0``;  ` `     `  `        ``// Store copy of the given number  ` `        ``int` `x = n;  ` `     `  `        ``// Store sum of the factorials  ` `        ``// of the digits  ` `        ``while` `(x != ``0``)  ` `        ``{  ` `            ``sum += fac[x % ``10``];  ` `            ``x /= ``10``;  ` `        ``}  ` `     `  `        ``// If it is divisible  ` `        ``if` `(sum % n == ``0``)  ` `            ``return` `true``;  ` `     `  `        ``return` `false``;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `main (String[] args)  ` `    ``{  ` `        ``int` `n = ``19``;  ` `     `  `        ``if` `(isPossible(n))  ` `            ``System.out.println(``"Yes"``);  ` `        ``else` `            ``System.out.println(``"No"``);  ` `     `  `    ``}  ` `} ` ` `  `// This code is contributed by Ryuga `

## Python3

 `# Python 3 implementation of the approach ` ` `  `# Function that returns true if n divides ` `# the sum of the factorials of its digits ` `def` `isPossible(n): ` `     `  `    ``# To store factorials of digits ` `    ``fac ``=` `[``0` `for` `i ``in` `range``(``10``)] ` `    ``fac[``0``] ``=` `1` `    ``fac[``1``] ``=` `1` ` `  `    ``for` `i ``in` `range``(``2``, ``10``, ``1``): ` `        ``fac[i] ``=` `fac[i ``-` `1``] ``*` `i ` ` `  `    ``# To store sum of the factorials ` `    ``# of the digits ` `    ``sum` `=` `0` ` `  `    ``# Store copy of the given number ` `    ``x ``=` `n ` ` `  `    ``# Store sum of the factorials ` `    ``# of the digits ` `    ``while` `(x): ` `        ``sum` `+``=` `fac[x ``%` `10``] ` `        ``x ``=` `int``(x ``/` `10``) ` ` `  `    ``# If it is divisible ` `    ``if` `(``sum` `%` `n ``=``=` `0``): ` `        ``return` `True` ` `  `    ``return` `False` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``n ``=` `19` ` `  `    ``if` `(isPossible(n)): ` `        ``print``(``"Yes"``) ` `    ``else``: ` `        ``print``(``"No"``) ` ` `  `# This code is contributed by ` `# Surendra_Gangwar `

## C#

 `// C# implementation of the approach  ` `using` `System; ` `class` `GFG  ` `{ ` `     `  `    ``// Function that returns true if n divides  ` `    ``// the sum of the factorials of its digits  ` `    ``static` `bool` `isPossible(``int` `n)  ` `    ``{  ` `     `  `        ``// To store factorials of digits  ` `        ``int``[] fac = ``new` `int``;  ` `        ``fac = fac = 1;  ` `     `  `        ``for` `(``int` `i = 2; i < 10; i++)  ` `            ``fac[i] = fac[i - 1] * i;  ` `     `  `        ``// To store sum of the factorials  ` `        ``// of the digits  ` `        ``int` `sum = 0;  ` `     `  `        ``// Store copy of the given number  ` `        ``int` `x = n;  ` `     `  `        ``// Store sum of the factorials  ` `        ``// of the digits  ` `        ``while` `(x != 0)  ` `        ``{  ` `            ``sum += fac[x % 10];  ` `            ``x /= 10;  ` `        ``}  ` `     `  `        ``// If it is divisible  ` `        ``if` `(sum % n == 0)  ` `            ``return` `true``;  ` `     `  `        ``return` `false``;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `Main ()  ` `    ``{  ` `        ``int` `n = 19;  ` `     `  `        ``if` `(isPossible(n))  ` `            ``Console.WriteLine(``"Yes"``);  ` `        ``else` `            ``Console.WriteLine(``"No"``);  ` `    ``}  ` `} ` ` `  `// This code is contributed by Code_Mech. `

## PHP

 ` `

Output:

```Yes
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.