Updated March 27, 2023

## Definition of Strong Number in Java

A strong number is a special number that can be defined as an addition of factorial of each digit of the number, which is equal to the number itself. To better understand the concept of a strong number, have a look at the below example:

The number 145 is a strong number. This is because if we add the factorials of each digit of this number, you will get the number, which is 145 itself, as the sum. 1! + 4! + 5! = 1 + 24 + 120 = 145.

Let us now have a look at the logic of checking if a number is a strong number or not in Java. Below is the description of checking if a number is a strong number or not.

- Take a user-defined number or a number as an input from the user. Store this number in a user-defined variable. Now copy this number to another temporary user-defined variable. This variable will be used for calculation purposes. You can name the user-defined variable as ‘n’ and the temporary variable as ‘temp_n’.
- Now initialize another variable that will store the sum of factorial digits. You can name this variable as ‘total’.
- You will also need to find the last digit of the given number ‘n’. Store this result in a variable that will store the result, say ‘lastdig = n % 10’.
- Now you can find the factorial of ‘lastdig’. Now you can store the factorial of this number and name it as ‘fact_n’.
- Once this is done, you can add the factorial to ‘total’. This can be done by using total = total + fact_n.
- Now you can remove the last digit from ‘n’ as it will not be required any further.
- Now repeat the steps from step 3 to step 6 until the condition n > 0 is satisfied.
- You can use a loop for this. It can be used to check the condition of a strong number. If the condition total == temp_n is satisfied, then the given number is a strong number, else it is not.

### Examples of Strong Number in Java

It is very easy to implement the logic of a strong number in Java; let us look at a few examples below.

#### Example #1

**Code:**

```
// Program in Java to check if a given number is a strong number or not
import java.util.*;
public class Main
{
public static void main(String[] args) {
int n,i;
int fact_n,lastdig;
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter the number : " );
n = sc.nextInt();
int total = 0;
int temp_n = n;
while(n != 0)
{
i = 1;
fact_n = 1;
lastdig = n % 10;
while(i <= lastdig)
{
fact_n = fact_n * i;
i++;
}
total = total + fact_n;
n = n / 10;
}
if(total == temp_n)
System.out.println(temp_n + " is a strong number\n");
else
System.out.println(temp_n + " is not a strong number\n");
System.out.println();
}
}
```

**Output:**

The above Java program has all the variables as discussed in the logic for the program. It first finds the mod or the last digit of the number until the number is not zero. Once this is done, it increments the number and stores it in fact_n to find out the factorial of the number. The total or addition is not in a while loop where the factorials are added until the condition of i<= lastdig is not satisfied. After the addition of ‘total’ and the fact_n is not equal to ‘temp_n’, then the given number is not factorial. On the other hand, if the total and fact_n is the same, then the number mentioned is s strong number.

#### Example #2

Another way of finding a strong number is by making use of strong numbers. The number here can be passed to the program by using the command line parameter.

**Code:**

```
public class Main
{
static int f[] = new int[10];
// Finding factorial for number 0 to 9
static void FirstCompute()
{
f[0] = f[1] = 1;
for (int i = 2; i<10; ++i)
f[i] = f[i-1] * i;
}
// If x is strong true is returned
static boolean isStrong(int x)
{
int Sum_fact = 0;
// Traverse through all digits of x.
int temp_n = x;
while (temp_n>0)
{
Sum_fact += f[temp_n%10];
temp_n /= 10;
}
return (Sum_fact == x);
}
// main function
public static void main (String[] args)
{
// calling preCompute
FirstCompute();
// first pass
int x = 145;
if(isStrong(x))
{
System.out.println("Yes");
}
else
System.out.println("No");
// second pass
x = 534;
if(isStrong(x))
{
System.out.println("Yes");
}
else
System.out.println("No");
}
}
```

This is another way of finding out if the number is a strong number or not. Here you can take the input from the user while runtime by using the command line arguments. The logic here remains the same, the difference being that the input is sent as arguments at runtime. This program’s output will be either YES or NO, depending on the number being a strong number or not.

**Output:**

The program here is checking for two numbers, if they are strong numbers or not. Here it finds out the factorials of all digits from 0 to 9. The isStrong function then validates if the number is strong or not. It will traverse through all digits of the given number until the mod exists. The main function here validates if the precompute() function is giving proper results. Two number are accepted from a user, which results in checking and providing if the number is a strong number or not. The main function first calls the function, which computes the factorial of each digit. The isStrong() function then checks for the sum of each digit’s factorial. It also calculates it and returns the result in a Boolean value, which is true or false. In the main, only passing the number will provide the result as per the output which is mentioned. If the number is strong true will be returned by the isStrong function, and if it is not, then false will be returned.

### Conclusion

A strong number is a number whose sum of each digit’s factorial is the number itself. A normal java function, or a function using Boolean values or by just passing the numbers as arguments, a strong number can be identified. The logic uses simple operations like a mod, division, and addition.

### Recommended Articles

This is a guide to Strong Number in Java. Here we discuss the definition and program to check if a number is a strong number or not in java and different examples and its code implementation. You may also have a look at the following articles to learn more –