Write a program to print All Strong numbers 1 to 100000


The program starts with a for loop that iterates from 1 to 100000. For each iteration, it calls the isItStrong() method to check if the current number is a Strong Number or not. If it is, the program prints the number.

The isItStrong() method takes a number as an argument and checks whether it is a Strong Number or not. It first initializes no as the input number, and sum as 0. Then, it extracts each digit from right to left using the modulo operator and calculates its factorial using the fact() method. The factorial is added to the sum. The loop continues until there are no more digits left.

Finally, the method checks whether the sum is equal to the input num. If it is, the method returns true, indicating that the number is a Strong Number. If it is not, the method returns false. The fact() method calculates the factorial of a given number using a for loop.

Source Code

class AllStrong_Number
{
	public static void main(String[] args)
	{
		for (int i = 1; i <= 100000; i++)
		{
			if (isItStrong(i))
			{			
				System.out.println(i);
			}
		}
	}
	static boolean isItStrong(int num) {
		int no = num;
		int sum = 0;
		while (no > 0) {
			int digit = no % 10;
			sum += fact(digit);
 
			no = no / 10;
		}
		return sum == num;
	}
	static int fact(int digit) {
		int f = 1;
		for (int j = digit; j > 1; j--) {
			f *= j;
		}
		return f;
	}
}

Output

1
2
145
40585

Example Programs