# Factorial of each element in Fibonacci series

Given the upper limit limit, print factorials of all Fibonacci Numbers smaller than limit.

Examples :

```Input : limit = 20
Output : 1 1 1 2 6 120 40320 6227020800
Explanation :
Fibonacci series in this range is 0, 1, 1, 2,
3, 5, 8, 13. Factorials of these numbers are
output.

Input : 50
Output : 1 1 1 2 6 120 40320 6227020800
51090942171709440000
295232799039604140847618609643520000000```

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

We know simple factorial computations cause overflow very soon. Therefore we use factorials of large numbers.

One simple solution is to generate all Fibonacci numbers one by one and compute factorial of every generated number using method discussed in factorials of large numbers

An efficient solution is based on the fact that Fibonacci numbers are increasing order. So we use previously generated factorial to compute next factorial.

## C++

 `// CPP program to find factorial of each element ` `// of Fibonacci series ` `#include ` `using` `namespace` `std; ` ` `  `// Maximum number of digits in output ` `#define MAX 500 ` ` `  `// Finds and print factorial of n using ` `// factorial of prev (stored in prevFact[ ` `// 0...size-1] ` `void` `factorial(``int` `prevFact[], ``int` `&size, ` `                         ``int` `prev, ``int` `n); ` ` `  `// Prints factorials of all fibonacci ` `// numbers smaller than given limit. ` `void` `printfibFactorials(``int` `limit) ` `{ ` `   ``if` `(limit < 1) ` `      ``return``; ` ` `  `   ``// Initialize first three Fibonacci ` `   ``// numbers and print factorials of ` `   ``// first two numbers. ` `   ``int` `a = 1, b = 1, c = 2; ` `   ``cout << a << ``" "` `<< b << ``" "``; ` ` `  `   ``// prevFact[] stores factorial of ` `   ``// previous fibonacci number ` `   ``int` `prevFact[MAX]; ` `   ``prevFact[0] = 1; ` ` `  `   ``// Size is current size of prevFact[] ` `   ``int` `size = 1; ` ` `  `   ``// Standard Fibonacci number loop ` `   ``while` `(c < limit) ` `   ``{ ` `       ``factorial(prevFact, size, b, c); ` `       ``a = b; ` `       ``b = c; ` `       ``c = a + b; ` `   ``} ` `} ` ` `  `// Please refer below article for details of ` `// below two functions. ` `// https://www.geeksforgeeks.org/factorial-large-number/ ` ` `  `// Function used to find factorial ` `int` `multiply(``int` `x, ``int` `prevFact[], ``int` `size) ` `{ ` `    ``int` `carry = 0; ` `    ``for` `(``int` `i = 0; i < size; i++) { ` `        ``int` `prod = prevFact[i] * x + carry; ` `        ``prevFact[i] = prod % 10; ` `        ``carry = prod / 10; ` `    ``} ` ` `  `    ``// Put carry in res and increase ` `    ``// result size ` `    ``while` `(carry) { ` `        ``prevFact[size] = carry % 10; ` `        ``carry = carry / 10; ` `        ``size++; ` `    ``} ` `    ``return` `size; ` `} ` ` `  `// Finds factorial of n using factorial ` `// "prev" stored in prevFact[]. size is ` `// size of prevFact[] ` `void` `factorial(``int` `prevFact[], ``int` `&size, ` `                         ``int` `prev, ``int` `n) ` `{ ` `   ``for` `(``int` `x = prev+1; x <= n; x++) ` `       ``size = multiply(x, prevFact, size); ` ` `  `    ``for` `(``int` `i = size - 1; i >= 0; i--) ` `        ``cout << prevFact[i]; ` `    ``cout << ``" "``; ` `} ` ` `  `// Driver function ` `int` `main() ` `{ ` `    ``int` `limit = 20; ` `    ``printfibFactorials(limit); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find  ` `// factorial of each element ` `// of Fibonacci series ` `import` `java.io.*; ` ` `  `class` `GFG ` `{      ` `    ``// Maximum number of ` `    ``// digits in output ` `    ``static` `int` `MAX = ``500``; ` `    ``static` `int` `size = ``1``; ` `     `  `    ``// Finds and print factorial  ` `    ``// of n using factorial of  ` `    ``// prev (stored in prevFact[ ` `    ``// 0...size-1] ` `    ``// Finds factorial of n  ` `    ``// using factorial "prev"  ` `    ``// stored in prevFact[]. size ` `    ``// is size of prevFact[] ` `    ``static` `void` `factorial(``int` `[]prevFact,  ` `                          ``int` `prev, ``int` `n) ` `    ``{ ` `    ``for` `(``int` `x = prev + ``1``;  ` `             ``x <= n; x++) ` `        ``size = multiply(x, prevFact, size); ` `     `  `        ``for` `(``int` `i = size - ``1``; ` `                 ``i >= ``0``; i--) ` `            ``System.out.print(prevFact[i]); ` `        ``System.out.print(``" "``); ` `    ``} ` `     `  `    ``// Prints factorials of all  ` `    ``// fibonacci numbers smaller  ` `    ``// than given limit. ` `    ``static` `void` `printfibFactorials(``int` `limit) ` `    ``{ ` `        ``if` `(limit < ``1``) ` `            ``return``; ` `     `  `        ``// Initialize first three ` `        ``// Fibonacci numbers and ` `        ``// print factorials of ` `        ``// first two numbers. ` `        ``int` `a = ``1``, b = ``1``, c = ``2``; ` `        ``System.out.print(a + ``" "` `+ ` `                         ``b + ``" "``); ` `         `  `        ``// prevFact[] stores factorial  ` `        ``// of previous fibonacci number ` `        ``int` `[]prevFact = ``new` `int``[MAX]; ` `        ``prevFact[``0``] = ``1``; ` `         `  `        ``// Standard Fibonacci ` `        ``// number loop ` `        ``while` `(c < limit) ` `        ``{ ` `            ``factorial(prevFact, b, c); ` `            ``a = b; ` `            ``b = c; ` `            ``c = a + b; ` `        ``} ` `    ``} ` `     `  `    ``// Please refer below ` `    ``// article for details of ` `    ``// below two functions. ` `    ``// https://www.geeksforgeeks.org/factorial-large-number/ ` `     `  `    ``// Function used to  ` `    ``// find factorial ` `    ``static` `int` `multiply(``int` `x, ` `                        ``int` `[]prevFact, ` `                        ``int` `size) ` `    ``{ ` `        ``int` `carry = ``0``; ` `        ``for` `(``int` `i = ``0``; i < size; i++)  ` `        ``{ ` `            ``int` `prod = prevFact[i] *  ` `                        ``x + carry; ` `            ``prevFact[i] = prod % ``10``; ` `            ``carry = prod / ``10``; ` `        ``} ` `     `  `        ``// Put carry in ` `        ``// res and increase ` `        ``// result size ` `        ``while` `(carry != ``0``)  ` `        ``{ ` `            ``prevFact[size] = carry % ``10``; ` `            ``carry = carry / ``10``; ` `            ``size++; ` `        ``} ` `        ``return` `size; ` `    ``} ` `     `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``int` `limit = ``20``; ` `        ``printfibFactorials(limit); ` `    ``} ` `} ` ` `  `// This code is contributed by  ` `// Manish Shaw(manishshaw1) `

## Python3

 `# Python3 program to find  ` `# factorial of each element  ` `# of Fibonacci series  ` ` `  `# Maximum number of  ` `# digits in output  ` `MAX` `=` `500` `size ``=` `1` ` `  `# Finds and print factorial  ` `# of n using factorial of  ` `# prev (stored in prevFact[  ` `# 0...size-1]  ` `# Finds factorial of n  ` `# using factorial "prev"  ` `# stored in prevFact[]. size  ` `# is size of prevFact[]  ` `def` `factorial(prevFact, prev,n) : ` `    ``global` `size ` `    ``for` `x ``in` `range``((prev ``+` `1``), n ``+` `1``) : ` `        ``size ``=` `multiply(x, prevFact, size) ` `         `  `    ``for` `i ``in` `range``((size ``-` `1``), ``-``1``, ``-``1``) : ` `        ``print``(prevFact[i], end ``=` `"")  ` `    ``print``(end ``=` `" "``) ` `     `  `     `  `# Prints factorials of all  ` `# fibonacci numbers smaller  ` `# than given limit.  ` `def` `printfibFactorials(limit) : ` `    ``if` `(limit < ``1``) : ` `        ``return` ` `  `    ``# Initialize first three  ` `    ``# Fibonacci numbers and  ` `    ``# print factorials of  ` `    ``# first two numbers.  ` `    ``a ``=` `1` `    ``b ``=` `1` `    ``c ``=` `2` `    ``print``(a,b , end ``=` `" "``) ` `     `  `    ``# prevFact[] stores factorial  ` `    ``# of previous fibonacci number  ` `    ``prevFact ``=` `[``0``] ``*` `MAX` `    ``prevFact[``0``] ``=` `1` `     `  `    ``# Standard Fibonacci  ` `    ``# number loop  ` `    ``while` `(c < limit) : ` `        ``factorial(prevFact, b, c)  ` `        ``a ``=` `b ` `        ``b ``=` `c  ` `        ``c ``=` `a ``+` `b  ` `     `  `# Please refer below  ` `# article for details of  ` `# below two functions.  ` `# https://www.geeksforgeeks.org/factorial-large-number/  ` `     `  `# Function used to  ` `# find factorial  ` `def` `multiply(x,prevFact,size) : ` `    ``carry ``=` `0` `    ``for` `i ``in` `range``(``0``, size) : ` `        ``prod ``=` `prevFact[i] ``*``x ``+` `carry  ` `        ``prevFact[i] ``=` `prod ``%` `10` `        ``carry ``=` `prod ``/``/` `10` `     `  `    ``# Put carry in  ` `    ``# res and increase  ` `    ``# result size  ` `    ``while` `(carry !``=` `0``) : ` `        ``prevFact[size] ``=` `carry ``%` `10` `        ``carry ``=` `carry ``/``/` `10` `        ``size ``=` `size ``+` `1` `     `  `    ``return` `size ` ` `  `     `  `# Driver Code  ` `limit ``=` `20` `printfibFactorials(limit) ` ` `  `# This code is contributed by Nikita Tiwari. `

## C#

 `// C# program to find  ` `// factorial of each element ` `// of Fibonacci series ` `using` `System; ` ` `  `class` `GFG ` `{      ` `    ``// Maximum number of ` `    ``// digits in output ` `    ``static` `int` `MAX = 500; ` `     `  `    ``// Finds and print factorial  ` `    ``// of n using factorial of  ` `    ``// prev (stored in prevFact[ ` `    ``// 0...size-1] ` `    ``// Finds factorial of n  ` `    ``// using factorial "prev"  ` `    ``// stored in prevFact[]. size ` `    ``// is size of prevFact[] ` `    ``static` `void` `factorial(``int` `[]prevFact,  ` `                          ``ref` `int` `size, ` `                          ``int` `prev, ``int` `n) ` `    ``{ ` `    ``for` `(``int` `x = prev + 1; x <= n; x++) ` `        ``size = multiply(x, prevFact, size); ` `     `  `        ``for` `(``int` `i = size - 1; i >= 0; i--) ` `            ``Console.Write(prevFact[i]); ` `        ``Console.Write(``" "``); ` `    ``} ` `     `  `    ``// Prints factorials of all fibonacci ` `    ``// numbers smaller than given limit. ` `    ``static` `void` `printfibFactorials(``int` `limit) ` `    ``{ ` `    ``if` `(limit < 1) ` `        ``return``; ` `     `  `    ``// Initialize first three Fibonacci ` `    ``// numbers and print factorials of ` `    ``// first two numbers. ` `    ``int` `a = 1, b = 1, c = 2; ` `    ``Console.Write(a + ``" "` `+ b + ``" "``); ` `     `  `    ``// prevFact[] stores factorial of ` `    ``// previous fibonacci number ` `    ``int` `[]prevFact = ``new` `int``[MAX]; ` `    ``prevFact[0] = 1; ` `     `  `    ``// Size is current size ` `    ``// of prevFact[] ` `    ``int` `size = 1; ` `     `  `    ``// Standard Fibonacci ` `    ``// number loop ` `    ``while` `(c < limit) ` `    ``{ ` `        ``factorial(prevFact, ``ref` `size, b, c); ` `        ``a = b; ` `        ``b = c; ` `        ``c = a + b; ` `    ``} ` `    ``} ` `     `  `    ``// Please refer below ` `    ``// article for details of ` `    ``// below two functions. ` `    ``// https://www.geeksforgeeks.org/factorial-large-number/ ` `     `  `    ``// Function used to find factorial ` `    ``static` `int` `multiply(``int` `x, ` `                        ``int` `[]prevFact, ``int` `size) ` `    ``{ ` `        ``int` `carry = 0; ` `        ``for` `(``int` `i = 0; i < size; i++)  ` `        ``{ ` `            ``int` `prod = prevFact[i] *  ` `                          ``x + carry; ` `            ``prevFact[i] = prod % 10; ` `            ``carry = prod / 10; ` `        ``} ` `     `  `        ``// Put carry in ` `        ``// res and increase ` `        ``// result size ` `        ``while` `(carry != 0)  ` `        ``{ ` `            ``prevFact[size] = carry % 10; ` `            ``carry = carry / 10; ` `            ``size++; ` `        ``} ` `        ``return` `size; ` `    ``} ` `     `  `    ``// Driver Code ` `    ``static` `void` `Main() ` `    ``{ ` `        ``int` `limit = 20; ` `        ``printfibFactorials(limit); ` `    ``} ` `} ` ` `  `// This code is contributed by  ` `// Manish Shaw(manishshaw1) `

## PHP

 `= 0; ``\$i``--) ` `            ``echo` `\$prevFact``[``\$i``]; ` `        ``echo` `" "``; ` `    ``} ` `     `  `// Prints factorials of all  ` `// fibonacci numbers smaller  ` `// than given limit. ` `function` `printfibFactorials(``\$limit``) ` `    ``{ ` `        ``global` `\$MAX``, ``\$prevFact``; ` `        ``if` `(``\$limit` `< 1) ` `            ``return``; ` `     `  `        ``// Initialize first three ` `        ``// Fibonacci numbers and ` `        ``// print factorials of ` `        ``// first two numbers. ` `        ``\$a` `= 1; ` `        ``\$b` `= 1; ` `        ``\$c` `= 2; ` `        ``echo` `\$a` `. ``" "` `. ``\$b` `. ``" "``; ` `         `  `        ``// prevFact[] stores factorial  ` `        ``// of previous fibonacci number ` `        ``\$prevFact``[0] = 1; ` `         `  `        ``// Standard Fibonacci ` `        ``// number loop ` `        ``while` `(``\$c` `< ``\$limit``) ` `        ``{ ` `            ``factorial(``\$b``, ``\$c``); ` `            ``\$a` `= ``\$b``; ` `            ``\$b` `= ``\$c``; ` `            ``\$c` `= ``\$a` `+ ``\$b``; ` `        ``} ` `    ``} ` `     `  `// Function used to  ` `// find factorial ` `function` `multiply(``\$x``,``\$size``) ` `    ``{ ` `        ``global` `\$prevFact``; ` `        ``\$carry` `= 0; ` `        ``for` `(``\$i` `= 0;  ` `             ``\$i` `< ``\$size``; ``\$i``++)  ` `        ``{ ` `            ``\$prod` `= ``\$prevFact``[``\$i``] *  ` `                    ``\$x` `+ ``\$carry``; ` `            ``\$prevFact``[``\$i``] = ``\$prod` `% 10; ` `            ``\$carry` `= (int)(``\$prod` `/ 10); ` `        ``} ` `     `  `        ``// Put carry in ` `        ``// res and increase ` `        ``// result size ` `        ``while` `(``\$carry` `!= 0)  ` `        ``{ ` `            ``\$prevFact``[``\$size``] = ``\$carry` `% 10; ` `            ``\$carry` `= (int)(``\$carry` `/ 10); ` `            ``\$size``++; ` `        ``} ` `        ``return` `\$size``; ` `    ``} ` `     `  `// Driver Code ` `\$limit` `= 20; ` `printfibFactorials(``\$limit``); ` ` `  `// This code is contributed  ` `// by mits ` `?> `

Output :

```1 1 2 6 120 40320 6227020800
```

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

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.