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:
ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock TestsThe 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.
It is very easy to implement the logic of a strong number in Java; let us look at a few examples below.
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.
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.
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.
The above is the detailed content of Strong Number in Java. For more information, please follow other related articles on the PHP Chinese website!