Java program to check if a number is strong or not :
A number is called a strong number if the sum of factorials of each digit is equal to the number. In this tutorial, we will write one java program to find out all strong numbers from 1 to 100000. You can modify this program to get an input from the user and test if it is strong or not.

Using a for loop, we will check each number from 1 to 100000 if it is a strong number.

Method isItStrong(int) is using here to find out if a number is strong or not

Method getFactorial(int) is using here to find out the factorial of a number.

First of all, we are getting the rightmost digit of the number using ’%’ operator and calculating the factorial.

Sum of factorials is stored in a variable sum

On each iteration, the right most digit of the number is removed by dividing it by 10

Finally, after the while loop ends, the total sum is checked with the number. If sum is equal to the number, it is a strong number .

If the number is detected as strong, return true. false otherwise.

If a number is strong, store it in a list.

After the check is done for all numbers, print out the list.
Program :
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Object> list = new ArrayList<>();
for (int i = 1; i <= 100000; i++) {
if (isItStrong(i)) {
//if the number is strong, store it in the ArrayList
list.add(i);
}
}
System.out.print(list);
}
/**
* check if the number is strong or not
*
* @param userInputNo : input number
* @return : true if userInputNo is strong
*/
static boolean isItStrong(int userInputNo) {
int no = userInputNo;
int sum = 0;
while (no > 0) {
int digit = no % 10;
sum += getFactorial(digit);
no = no / 10;
}
return sum == userInputNo;
}
/**
* get factorial of a number
*
* @param digit : number to find factorial
* @return : factorial of digit
*/
static int getFactorial(int digit) {
int fact = 1;
for (int j = digit; j > 1; j) {
fact *= j;
}
return fact;
}
}
It will print the below output:
[1, 2, 145, 40585]
That’s all about strong number and how to find it using Java. Please drop us a comment below if you have any question on this tutorial.
Similar tutorials :
 Java program to find the number of vowels and digits in a String
 Java Program to reverse a number
 Java Program to check if a number is Neon or not
 Java program to get the maximum number holder Student
 Java example to find missing number in an array of sequence
 How to add zeros to the start of a number in Java