Given a number **N**, print all the Strong Numbers less than or equal to **N**.

Strong numberis a special number whose sum of the factorial of digits is equal to the original number.

For Example: 145 is strong number. Since, 1! + 4! + 5! = 145.

**Examples:**

Input:N = 100Output:1 2Explanation:

Only 1 and 2 are the strong numbers from 1 to 100 because

1! = 1, and

2! = 2Input:N = 1000Output:1 2 145Explanation:

Only 1, 2 and 145 are the strong numbers from 1 to 1000 because

1! = 1,

2! = 2, and

(1! + 4! + 5!) = 145

**Approach:** The idea is to iterate from **[1, N]** an check if any number between the range is strong number or not. If yes then print the corresponding number, else check for next number.

Below is the implementation of the above approach:

## Java

`// Java program for the above approach` `class` `GFG {` ` ` `// Store the factorial of all the` ` ` `// digits from [0, 9]` ` ` `static` `int` `[] factorial = { ` `1` `, ` `1` `, ` `2` `, ` `6` `, ` `24` `, ` `120` `,` ` ` `720` `, ` `5040` `, ` `40320` `, ` `362880` `};` ` ` `// Function to return true` ` ` `// if number is strong or not` ` ` `public` `static` `boolean` `isStrong(` `int` `N)` ` ` `{` ` ` `// Converting N to String so that` ` ` `// can easily access all it's digit` ` ` `String num = Integer.toString(N);` ` ` `// sum will store summation of` ` ` `// factorial of all digits` ` ` `// of a number N` ` ` `int` `sum = ` `0` `;` ` ` `for` `(` `int` `i = ` `0` `;` ` ` `i < num.length(); i++) {` ` ` `sum += factorial[Integer` ` ` `.parseInt(num` ` ` `.charAt(i)` ` ` `+ ` `""` `)];` ` ` `}` ` ` `// Returns true of N is strong number` ` ` `return` `sum == N;` ` ` `}` ` ` `// Function to print all` ` ` `// strong number till N` ` ` `public` `static` `void` ` ` `printStrongNumbers(` `int` `N)` ` ` `{` ` ` `// Iterating from 1 to N` ` ` `for` `(` `int` `i = ` `1` `; i <= N; i++) {` ` ` `// Checking if a number is` ` ` `// strong then print it` ` ` `if` `(isStrong(i)) {` ` ` `System.out.print(i + ` `" "` `);` ` ` `}` ` ` `}` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` ` ` `main(String[] args)` ` ` `throws` `java.lang.Exception` ` ` `{` ` ` `// Given Number` ` ` `int` `N = ` `1000` `;` ` ` `// Function Call` ` ` `printStrongNumbers(N);` ` ` `}` `}` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG{` `// Store the factorial of all the` `// digits from [0, 9]` `static` `int` `[] factorial = { 1, 1, 2, 6, 24, 120,` ` ` `720, 5040, 40320, 362880 };` `// Function to return true` `// if number is strong or not` `public` `static` `bool` `isStrong(` `int` `N)` `{` ` ` `// Converting N to String so that` ` ` `// can easily access all it's digit` ` ` `String num = N.ToString();` ` ` `// sum will store summation of` ` ` `// factorial of all digits` ` ` `// of a number N` ` ` `int` `sum = 0;` ` ` `for` `(` `int` `i = 0; i < num.Length; i++) ` ` ` `{` ` ` `sum += factorial[` `int` `.Parse(num[i] + ` `""` `)];` ` ` `}` ` ` `// Returns true of N is strong number` ` ` `return` `sum == N;` `}` `// Function to print all` `// strong number till N` `public` `static` `void` `printStrongNumbers(` `int` `N)` `{` ` ` `// Iterating from 1 to N` ` ` `for` `(` `int` `i = 1; i <= N; i++) ` ` ` `{` ` ` `// Checking if a number is` ` ` `// strong then print it` ` ` `if` `(isStrong(i)) ` ` ` `{` ` ` `Console.Write(i + ` `" "` `);` ` ` `}` ` ` `}` `}` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` `// Given Number` ` ` `int` `N = 1000;` ` ` `// Function Call` ` ` `printStrongNumbers(N);` `}` `}` `// This code is contributed by sapnasingh4991` |

*chevron_right*

*filter_none*

**Output:**

1 2 145

**Time Complexity:** *O(N)*

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 Jumping Numbers smaller than or equal to a given value
- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- Check if N is Strong Prime
- Program to check Strong Number
- Print all non-increasing sequences of sum equal to a given number x
- Print all proper fractions with denominators less than equal to N
- Count numbers whose XOR with N is equal to OR with N
- Making three numbers equal with the given operations
- Minimum numbers (smaller than or equal to N) with sum S
- Find all factorial numbers less than or equal to n
- Sum of Semi-Prime Numbers less than or equal to N
- Different ways to sum n using numbers greater than or equal to m
- Split N^2 numbers into N groups of equal sum
- Make all numbers of an array equal
- Split numbers from 1 to N into two equal sum subsets
- Count numbers whose difference with N is equal to XOR with N
- Place N^2 numbers in matrix such that every row has an equal sum
- Count the numbers < N which have equal number of divisors as K

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.