Print all Strong numbers less than or equal to N | Java 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
class GFG { static int [ ] factorial = { 1 , 1 , 2 , 6 , 24 , 120 , 720 , 5040 , 40320 , 362880 } ; public static boolean isStrong ( int N ) { String num = Integer . toString ( N ) ; int sum = 0 ; for ( int i = 0 ; i < num . length ( ) ; i ++ ) { sum += factorial [ Integer . parseInt ( num . charAt ( i ) + " " ) ] ; } return sum == N ; } public static void printStrongNumbers ( int N ) { for ( int i = 1 ; i <= N ; i ++ ) { if ( isStrong ( i ) ) { System . out . print ( i + " ▁ " ) ; } } } public static void main ( String [ ] args ) throws java . lang . Exception { int N = 1000 ; printStrongNumbers ( N ) ; } }