# Print all Strong numbers less than or equal to N

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

Strong number is 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 = 100
Output: 1 2
Explanation:
Only 1 and 2 are the strong numbers from 1 to 100 because
1! = 1, and
2! = 2
Input: N = 1000
Output: 1 2 145
Explanation:
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);` `    ``}` `}`

## 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`

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.

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.