A number N is called a factorial number if it is the factorial of a positive integer. For example, the first few factorial numbers are

1, 2, 6, 24, 120, …

Given a number n, print all factorial numbers smaller than or equal to n.

Examples :

Input : n = 100 Output : 1 2 6 24 Input : n = 1500 Output : 1 2 6 24 120 720

A **simple solution **is to generate all factorials one by one until the generated factorial is greater than n.

An **efficient solution** is to find next factorial using previous factorial.

## C++

`// CPP program to find all factorial numbers ` `// smaller than or equal to n. ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `void` `printFactorialNums(` `int` `n) ` `{ ` ` ` `int` `fact = 1; ` ` ` `int` `x = 2; ` ` ` `while` `(fact <= n) { ` ` ` `cout << fact << ` `" "` `; ` ` ` ` ` `// Compute next factorial ` ` ` `// using previous ` ` ` `fact = fact * x; ` ` ` ` ` `x++; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 100; ` ` ` `printFactorialNums(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find all factorial numbers ` `// smaller than or equal to n. ` ` ` `class` `GFG ` `{ ` ` ` `static` `void` `printFactorialNums(` `int` `n) ` ` ` `{ ` ` ` `int` `fact = ` `1` `; ` ` ` `int` `x = ` `2` `; ` ` ` `while` `(fact <= n) ` ` ` `{ ` ` ` `System.out.print(fact + ` `" "` `); ` ` ` ` ` `// Compute next factorial ` ` ` `// using previous ` ` ` `fact = fact * x; ` ` ` ` ` `x++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `100` `; ` ` ` `printFactorialNums(n); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find all factorial ` `# numbers smaller than or equal to n. ` ` ` `def` `printFactorialNums( n): ` ` ` `fact ` `=` `1` ` ` `x ` `=` `2` ` ` `while` `fact <` `=` `n: ` ` ` `print` `(fact, end ` `=` `" "` `) ` ` ` ` ` `# Compute next factorial ` ` ` `# using previous ` ` ` `fact ` `=` `fact ` `*` `x ` ` ` ` ` `x ` `+` `=` `1` ` ` `# Driver code ` `n ` `=` `100` `printFactorialNums(n) ` ` ` `# This code is contributed by "Abhishek Sharma 44" ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find all factorial numbers ` `// smaller than or equal to n. ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `static` `void` `printFactorialNums(` `int` `n) ` ` ` `{ ` ` ` `int` `fact = 1; ` ` ` `int` `x = 2; ` ` ` `while` `(fact <= n) ` ` ` `{ ` ` ` `Console.Write(fact + ` `" "` `); ` ` ` ` ` `// Compute next factorial ` ` ` `// using previous ` ` ` `fact = fact * x; ` ` ` ` ` `x++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 100; ` ` ` `printFactorialNums(n); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find all ` `// factorial numbers smaller ` `// than or equal to n. ` ` ` `function` `printFactorialNums(` `$n` `) ` `{ ` ` ` `$fact` `= 1; ` ` ` `$x` `= 2; ` ` ` `while` `(` `$fact` `<= ` `$n` `) ` ` ` `{ ` ` ` `echo` `$fact` `, ` `" "` `; ` ` ` ` ` `// Compute next factorial ` ` ` `// using previous ` ` ` `$fact` `= ` `$fact` `* ` `$x` `; ` ` ` `$x` `++; ` ` ` `} ` `} ` ` ` ` ` `// Driver code ` ` ` `$n` `= 100; ` ` ` `echo` `printFactorialNums(` `$n` `); ` ` ` `// This code is contributed by ajit. ` `?> ` |

*chevron_right*

*filter_none*

Output:

1 2 6 24

Time Complexity : O(n)

If there are multiple queries, then we can cache all previously computed factorial numbers to avoid re-computations.

This article is contributed by **Shubham Sagar**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

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.

## Recommended Posts:

- Print all Semi-Prime Numbers less than or equal to N
- Print all Strong numbers less than or equal to N
- Find the last digit when factorial of A divides factorial of B
- Highest and Smallest power of K less than and greater than equal to N respectively
- Find maximum product of digits among numbers less than or equal to N
- Find all powers of 2 less than or equal to a given number
- Find all possible subarrays having product less than or equal to K
- C++ program to find all numbers less than n, which are palindromic in base 10 and base 2.
- Sum of Semi-Prime Numbers less than or equal to N
- Print all proper fractions with denominators less than equal to N
- Find unique pairs such that each element is less than or equal to N
- Find Largest Special Prime which is less than or equal to a given number
- Find three integers less than or equal to N such that their LCM is maximum
- Find Multiples of 2 or 3 or 5 less than or equal to N
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Numbers less than N which are product of exactly two distinct prime numbers
- Generate all palindromic numbers less than n
- Count all the numbers less than 10^6 whose minimum prime factor is N
- Print all numbers less than N with at-most 2 unique digits
- Largest number with maximum trailing nines which is less than N and greater than N-D