Last Updated on May 31, 2023 by Mayank Dham
A number is said to be a Strong number if the sum of factorials of its digits equals the same input number. To be more specific, we must find the factorial of each digit of the given number. Then we must compute the sum of those factorials. Then we must compare the sum value and the input number; if they are the same, the given number is a strong number; otherwise, it is not. In this article, we’ll look at how to use the Java programming language to determine whether a number is a strong number.
Examples of Strong numbers
Eg 1:
The number entered is 145.
The sum of the factorials of 1, 4, and 5 is 1, 24, and 120.
The total of these factorials equals 1 + 24 + 120 = 145.
As we can see, the sum of the factorials and the input value are both the same. Hence, 145 is a strong number.
Eg 2:
The number entered is 534.
The factorial of 5, 3, and 4 is 120, 6, and 24 respectively.
The total of these factorials equals 120 + 6 + 24 = 150.
As we can see, the sum of the factorials and the input value are not the same thing. As a result, 534 is not a Strong number.
We will discuss various approaches to resolving this issue.
Method 1: Using iteration to find whether a number is a strong number or not in Java
- We’ll define a function that accepts a number and returns its factorial value.
- Using the modulo and divide operators, we divided the number into individual digits.
- We then use the user-defined function to compute their factorial.
- We add up all of the factorial values and compare them to the original number.
Code Implementation
public class Main { static int facto (int n) { int fact = 1; for (int i = 1; i <= n; i++) fact = fact * i; return fact; } static boolean detectStrong (int num) { int digit, sum = 0; int temp = num; boolean flag = false; while (temp != 0) { digit = temp % 10; sum = sum + facto (digit); temp /= 10; } if (sum == num) flag = true; else flag = false; return flag; } public static void main (String[]args) { int num = 145; if (detectStrong (num)) System.out.println (num + " is Strong Number"); else System.out.println (num + " is not a Strong Number"); } }
Output
145 is Strong Number
Method 2: Using recursion to find whether a number is a strong number or not in Java
- First, we’ll define a recursive function that accepts a number as an argument and returns its factorial value.
- The number is then broken down using the modulo and divide operators to extract the digits and shorten the number, respectively.
- For each number, we then call the factorial function we declared earlier.
- Finally, we add up all of the factorial values and compare them to the original number.
Code Implementation
public class Main { public static void main (String[]args) { int num = 145; if (detectStrong (num)) System.out.println (num + " is Strong Number"); else System.out.println (num + " is not a Strong Number"); } static int facto (int num) { if(num == 0) return 1; return num * facto(num-1); } static boolean detectStrong (int num) { int digit, sum = 0; int temp = num; while(temp!=0){ digit = temp % 10; sum = sum + facto(digit); temp /= 10; } return sum == num; } }
Output
145 is Strong Number
Method 3: Using dynamic programming to find whether a number is a strong number or not in Java
- This method employs dynamic programming to compute factorial values in advance.
- This is advantageous because we may not have to recalculate factorials for new digits repeatedly.
Code Implementation
public class Main { static int f[] = new int[10]; static void preComputer() { f[0] = f[1] = 1; for (int i = 2; i<10; ++i) f[i] = f[i-1] * i; } static boolean checkStrong(int num) { int sum = 0; int temp = num; while (temp > 0) { sum += f[temp % 10]; temp = temp / 10; } return (sum == num); } public static void main (String[] args) { preComputer(); int val = 145; if(checkStrong(val)) System.out.println(val + " is a strong number"); else System.out.println(val + " is not a strong number"); } }
Output
145 is a strong number
Conclusion
Finally, this article discussed various Java methods for determining whether a number is a strong number or not. Strong numbers have the property that the sum of their factorials equals the original number. The article demonstrated three approaches: iteration, recursion, and dynamic programming. Each method provides a method for calculating the factorials and comparing the sum to the input number. Programmers can easily identify strong numbers in Java by implementing these methods.
Frequently Asked Questions (FAQs)
Q1.In Java, how do you determine whether a number is strong or not?
You can do this with a loop. It can be used to determine the state of a strong number. If the condition total == temp_n is met, the given number is a strong number; otherwise, it is not.
Q2.How is 2 a strong number?
Strong numbers are those in which the sum of the factorials of each digit equals the original number.
Q3.Is the special number and the strong number the same?
A Strong number is a special number in which the sum of all digit factorials is equal to the number itself.