











Program to find Strong numbers in 1 to 100 in Java
Strong Number in 1 to 100 in Java
Here, in this page we will discuss the program to find Strong Number in 1 to 100 in Java. We will discuss the algorithm along its code in java


Strong Number
Strong number is a special number whose sum of the factorial of digits is equal to the original number.
Algorithm :
- Run a loop from [1, 100] and check if any number between the range is strong number or not.
- To check a number is strong or not,
- Store the factorial for digits 0 to 9 in an array.
- Run a loop till the number is greater than 0, set
- Sum = sum + (n%10)!.
- At last check if(sum==n)
- If yes then print the corresponding number, else check for the next number.
Time and Space Complexity :
- Time Complexity : O(n)
- Space Complexity : O(1)
Code in Java
Run
class Main{ 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 + " "); } } } // Driver Code public static void main(String[] args) { int N=100; // Function Call printStrongNumbers(N); } }
Output
1 2
Login/Signup to comment