# Find sum of digits in factorial of a number

Given a number n, write code to find the sum of digits in the factorial of the number. Given n <= 5000

Examples:

```Input : 10
Output : 27

Input : 100
Output : 648
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

It is not possible to store a number as large as 100! under some data types so, idea is to store extremely large number in vector.

```1) Create a vector to store factorial digits and
initialize it with 1.
2) One by one multiply numbers from 1 to n to
the vector. We use school mathematics for this
purpose.
3) Sum all the elements in vector and return the sum.
```

## C++

 `// C++ program to find sum of digits in factorial ` `// of a number ` `#include ` `using` `namespace` `std; ` ` `  `// Function to multiply x with large number ` `// stored in vector v. Result is stored in v. ` `void` `multiply(vector<``int``> &v, ``int` `x) ` `{ ` `    ``int` `carry = 0, res; ` `    ``int` `size = v.size(); ` `    ``for` `(``int` `i = 0 ; i < size ; i++) ` `    ``{ ` `        ``// Calculate res + prev carry ` `        ``int` `res = carry + v[i] * x; ` ` `  `        ``// updation at ith position ` `        ``v[i] = res % 10; ` `        ``carry = res / 10; ` `    ``} ` `    ``while` `(carry != 0) ` `    ``{ ` `        ``v.push_back(carry % 10); ` `        ``carry /= 10; ` `    ``} ` `} ` ` `  `// Returns sum of digits in n! ` `int` `findSumOfDigits(``int` `n) ` `{ ` `    ``vector<``int``> v;     ``// create a vector of type int ` `    ``v.push_back(1);    ``// adds 1 to the end ` ` `  `    ``// One by one multiply i to current vector ` `    ``// and update the vector. ` `    ``for` `(``int` `i=1; i<=n; i++) ` `        ``multiply(v, i); ` ` `  `    ``// Find sum of digits in vector v[] ` `    ``int` `sum = 0; ` `    ``int` `size = v.size(); ` `    ``for` `(``int` `i = 0 ; i < size ; i++) ` `        ``sum += v[i]; ` `    ``return` `sum; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 1000; ` `    ``cout << findSumOfDigits(n); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find sum of digits in factorial  ` `// of a number  ` `import` `java.util.*; ` `class` `GFG{ ` `// Function to multiply x with large number  ` `// stored in vector v. Result is stored in v. ` `static` `ArrayList v=``new` `ArrayList(); ` `static` `void` `multiply(``int` `x)  ` `{  ` `    ``int` `carry = ``0``;  ` `    ``int` `size = v.size();  ` `    ``for` `(``int` `i = ``0` `; i < size ; i++)  ` `    ``{  ` `        ``// Calculate res + prev carry  ` `        ``int` `res = carry + v.get(i) * x;  ` ` `  `        ``// updation at ith position  ` `        ``v.set(i,res % ``10``);  ` `        ``carry = res / ``10``;  ` `    ``}  ` `    ``while` `(carry != ``0``)  ` `    ``{  ` `        ``v.add(carry % ``10``);  ` `        ``carry /= ``10``;  ` `    ``}  ` `}  ` ` `  `// Returns sum of digits in n!  ` `static` `int` `findSumOfDigits(``int` `n)  ` `{  ` `    ``v.add(``1``); ``// adds 1 to the end  ` ` `  `    ``// One by one multiply i to current vector  ` `    ``// and update the vector.  ` `    ``for` `(``int` `i=``1``; i<=n; i++)  ` `        ``multiply(i);  ` ` `  `    ``// Find sum of digits in vector v[]  ` `    ``int` `sum = ``0``;  ` `    ``int` `size = v.size();  ` `    ``for` `(``int` `i = ``0` `; i < size ; i++)  ` `        ``sum += v.get(i);  ` `    ``return` `sum;  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `main(String[] args)  ` `{  ` `    ``int` `n = ``1000``;  ` `    ``System.out.println(findSumOfDigits(n));  ` `}  ` `} ` `// this code is contributed by mits `

## Python 3

 `# Python 3 program to find sum of digits ` `# in factorial of a number ` ` `  `# Function to multiply x with large number ` `# stored in vector v. Result is stored in v. ` `def` `multiply(v, x): ` `    ``carry ``=` `0` `    ``size ``=` `len``(v) ` `    ``for` `i ``in` `range``(size): ` `         `  `        ``# Calculate res + prev carry ` `        ``res ``=` `carry ``+` `v[i] ``*` `x ` ` `  `        ``# updation at ith position ` `        ``v[i] ``=` `res ``%` `10` `        ``carry ``=` `res ``/``/` `10` ` `  `    ``while` `(carry !``=` `0``): ` `        ``v.append(carry ``%` `10``) ` `        ``carry ``/``/``=` `10` ` `  `# Returns sum of digits in n! ` `def` `findSumOfDigits( n): ` `    ``v ``=` `[]     ``# create a vector of type int ` `    ``v.append(``1``) ``# adds 1 to the end ` ` `  `    ``# One by one multiply i to current  ` `    ``# vector and update the vector. ` `    ``for` `i ``in` `range``(``1``, n ``+` `1``): ` `        ``multiply(v, i) ` ` `  `    ``# Find sum of digits in vector v[] ` `    ``sum` `=` `0` `    ``size ``=` `len``(v) ` `    ``for` `i ``in` `range``(size): ` `        ``sum` `+``=` `v[i] ` `    ``return` `sum` ` `  `# Driver code ` `if` `__name__ ``=``=` `"__main__"``: ` `     `  `    ``n ``=` `1000` `    ``print``(findSumOfDigits(n)) ` ` `  `# This code is contributed by ita_c `

## C#

 `// C# program to find sum of digits in factorial  ` `// of a number  ` `using` `System; ` `using` `System.Collections; ` `class` `GFG{ ` `// Function to multiply x with large number  ` `// stored in vector v. Result is stored in v. ` `static` `ArrayList v=``new` `ArrayList(); ` `static` `void` `multiply(``int` `x)  ` `{  ` `    ``int` `carry = 0;  ` `    ``int` `size = v.Count;  ` `    ``for` `(``int` `i = 0 ; i < size ; i++)  ` `    ``{  ` `        ``// Calculate res + prev carry  ` `        ``int` `res = carry + (``int``)v[i] * x;  ` ` `  `        ``// updation at ith position  ` `        ``v[i] = res % 10;  ` `        ``carry = res / 10;  ` `    ``}  ` `    ``while` `(carry != 0)  ` `    ``{  ` `        ``v.Add(carry % 10);  ` `        ``carry /= 10;  ` `    ``}  ` `}  ` ` `  `// Returns sum of digits in n!  ` `static` `int` `findSumOfDigits(``int` `n)  ` `{  ` `    ``v.Add(1); ``// adds 1 to the end  ` ` `  `    ``// One by one multiply i to current vector  ` `    ``// and update the vector.  ` `    ``for` `(``int` `i=1; i<=n; i++)  ` `        ``multiply(i);  ` ` `  `    ``// Find sum of digits in vector v[]  ` `    ``int` `sum = 0;  ` `    ``int` `size = v.Count;  ` `    ``for` `(``int` `i = 0 ; i < size ; i++)  ` `        ``sum += (``int``)v[i];  ` `    ``return` `sum;  ` `}  ` ` `  `// Driver code  ` `static` `void` `Main()  ` `{  ` `    ``int` `n = 1000;  ` `    ``Console.WriteLine(findSumOfDigits(n));  ` `}  ` `} ` `// this code is contributed by mits `

## PHP

 ` `

Output:

```10539
```

This article is contributed by Sakshi Tiwari . If you like GeeksforGeeks(We know you do!) 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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Improved By : Mithun Kumar, chitranayal

Article Tags :
Practice Tags :

Be the First to upvote.

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