Write a program to check Strong numbers or Not


This Java program determines whether the given number is a Strong Number or not. A Strong Number is a number whose sum of the factorial of each digit is equal to the number itself. For example, 145 is a strong number because 1! + 4! + 5! = 1 + 24 + 120 = 145.

The program first takes input from the user for the number to be checked. Then, it initializes some variables such as i for the factorial, fact_n to store the factorial, rem to get the remainder of each digit, total to store the sum of factorials, and temp to store the original number.

The program then runs a loop that extracts each digit of the number from the right to the left, calculates its factorial, and adds it to the total. The loop continues until there are no more digits left.

Finally, the program checks whether the total is equal to the temp variable, which holds the original number. If it is, the program prints that the number is a Strong Number, else it prints that it is not.

Source Code

import java.util.Scanner;
class Strong_Number
{
	public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);		
        System.out.print("Enter The Number : ");
        int n = input.nextInt();
		int i,fact_n,rem,total = 0,temp = n;
		while(n != 0)
		{
			i = 1;
			fact_n = 1;
			rem = n % 10;
			while(i <= rem)
			{
				fact_n = fact_n * i;
				i++;
			}
			total = total + fact_n;
			n = n / 10;
		}
		if(total == temp)
			System.out.println(temp + " is a Strong Number");
		else
			System.out.println(temp + " is not a Strong Number");
     }
}
 

Output

Enter The Number : 10
10 is not a Strong Number

Example Programs