Print all Strong numbers less than or equal to N | C ++ program for the above approach ; Store the factorial of all the digits from [ 0 , 9 ] ; Function to return true if number is strong or not ; Converting N to String so that can easily access all it 's digit ; sum will store summation of factorial of all digits of a number N ; Returns true of N is strong number ; Function to print all strong number till N ; Iterating from 1 to N ; Checking if a number is strong then print it ; Driver Code ; Given number ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int factorial [ ] = { 1 , 1 , 2 , 6 , 24 , 120 , 720 , 5040 , 40320 , 362880 } ; bool isStrong ( int N ) { string num = to_string ( N ) ; int sum = 0 ; for ( int i = 0 ; i < num . length ( ) ; i ++ ) { sum += factorial [ num [ i ] - '0' ] ; } return sum == N ; } void printStrongNumbers ( int N ) { for ( int i = 1 ; i <= N ; i ++ ) { if ( isStrong ( i ) ) { cout << i << " ▁ " ; } } } int main ( ) { int N = 1000 ; printStrongNumbers ( N ) ; return 0 ; }