# Print all Strong numbers less than or equal to N

• Difficulty Level : Easy
• Last Updated : 15 Mar, 2021

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] and check if any number between the range is strong number or not. If yes then print the corresponding number, else check for the next number.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include``using` `namespace` `std;` `// Store the factorial of all the``// digits from [0, 9]``int` `factorial[] = { 1, 1, 2, 6, 24, 120,``                    ``720, 5040, 40320, 362880 };` `// Function to return true``// if number is strong or not``bool` `isStrong(``int` `N)``{``    ` `    ``// Converting N to String so that``    ``// can easily access all it's digit``    ``string num = to_string(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[num[i] - ``'0'``];``    ``}`` ` `    ``// Returns true of N is strong number``    ``return` `sum == N;``}`` ` `// Function to print all``// strong number till N``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))``        ``{``            ``cout << i << ``" "``;``        ``}``    ``}``}` `// Driver Code``int` `main()``{``    ` `    ``// Given number``    ``int` `N = 1000;``    ` `    ``// Function call``    ``printStrongNumbers(N);``    ` `    ``return` `0;``}` `// This code is contributed by rutvik_56`

## 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);``    ``}``}`

## Python3

 `# Python3 program for the``# above approach` `# Store the factorial of``# all the digits from [0, 9]``factorial ``=` `[``1``, ``1``, ``2``, ``6``, ``24``, ``120``,``             ``720``, ``5040``, ``40320``, ``362880``]` `# Function to return true``# if number is strong or not``def` `isStrong(N):` `    ``# Converting N to String``    ``# so that can easily access``    ``# all it's digit``    ``num ``=` `str``(N)`` ` `    ``# sum will store summation``    ``# of factorial of all``    ``# digits of a number N``    ``sum` `=` `0`` ` `    ``for` `i ``in` `range` `(``len``(num)):``        ``sum` `+``=` `factorial[``ord``(num[i]) ``-``                         ``ord``(``'0'``)]``  ` `    ``# Returns true of N``    ``# is strong number``    ``if` `sum` `=``=` `N:``       ``return` `True``    ``else``:``       ``return` `False`` ` `# Function to print all``# strong number till N``def` `printStrongNumbers(N):``    ` `    ``# Iterating from 1 to N``    ``for` `i ``in` `range` `(``1``, N ``+` `1``):``   ` `        ``# Checking if a number is``        ``# strong then print it``        ``if` `(isStrong(i)):``            ``print` `(i, end ``=` `" "``)` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:``  ` `    ``# Given number``    ``N ``=` `1000`` ` `    ``# Function call``    ``printStrongNumbers(N)``    ` `# This code is contributed by Chitranayal`

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

## Javascript

 ``

Output:

`1 2 145`

Time Complexity: O(N)

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.52668136,"math_prob":0.9883046,"size":5846,"snap":"2022-27-2022-33","text_gpt3_token_len":1873,"char_repetition_ratio":0.15765148,"word_repetition_ratio":0.47411668,"special_character_ratio":0.37495723,"punctuation_ratio":0.143108,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99970055,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-08-11T03:39:15Z\",\"WARC-Record-ID\":\"<urn:uuid:072cbacd-de75-4a1f-ac0e-bd3bfe8b07ed>\",\"Content-Length\":\"178198\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:4092d14b-7611-4e70-b070-c057f2db3837>\",\"WARC-Concurrent-To\":\"<urn:uuid:5d0dcad9-24eb-46cb-8987-c340e0ff9e28>\",\"WARC-IP-Address\":\"23.221.227.117\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/print-all-strong-numbers-less-than-or-equal-to-n/?ref=rp\",\"WARC-Payload-Digest\":\"sha1:W34QQQ2QXZUHLFOXA5GNFVTL4L3QL5QR\",\"WARC-Block-Digest\":\"sha1:EPG4QHIUFHTDFY6CUH6VPJIY4TQJLJGJ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-33/CC-MAIN-2022-33_segments_1659882571232.43_warc_CC-MAIN-20220811012302-20220811042302-00029.warc.gz\"}"}