Strong Number in Java

What is a Strong Number in Java?

Strong numbers, also called factorial numbers, are special numbers that have the property of the sum of the factorial of their digits being equal to the original number.

Strong numbers in Java Examples:

Let’s examine a few examples to further clarify the idea of strong numbers. Some Strong numbers are as follows:

  1. 145: As previously stated, the sum of its digit factorials (1! + 4! + 5!) equals the number itself.
  2. 40585: The sum of its factorials (4! + 0! + 5! + 8! + 5!) equals the number itself.
  3. 2: 2 is regarded as a strong number even though it is a single digit because 2! = 2.

Check whether it is a Strong Number or Not

import java.util.*;
public class Main {
   //main method
   public static void main(String[] args) {

      //declare an int variable and initialize a number as value
      int inputNum = 40585;

      //declare a variable for iteration
      int i;

      //declare variables for factorial value and the extracted digits
      int factorial,digit;

      //declare a variable to store the sum value
      int sum = 0;

      //transfer the input value to a temporary variable
      int temp = inputNum;

      //start looping for calculating the result
      while(temp != 0) {
         i = 1;
         factorial = 1;

         //extracting the digit
         digit = temp % 10;

         //get the factorial of the digit
         while(i <= digit) {
            factorial = factorial * i;

         //store the sum value
         sum = sum + factorial;

         //removing the digit one by one
         temp = temp / 10;

      //check condition
      if(sum == inputNum)
         //if sum value is equal to input number
         System.out.println(inputNum + " is a strong number\n");
         //if sum value is not equal to input number
         System.out.println(inputNum + " is not a strong number\n");

Explanation of the code:

An amount that equals the factorial sum of its digits is referred to as a strong number. The inputNum variable is first declared and initialized with a value of 40585 in the program’s opening statement. When determining whether a number is powerful, this one will be used. 

By taking the modulus of 10 for each digit of the inputNum inside the while loop, the computer can extract each digit. Following that, a further nested while loop calculates the digit’s factorial. A variable called “factorial” holds the factorial. By adding the factorial value, the sum variable is updated.

The software checks the sum variable with the initial inputNum once the while loop has finished. In the event that they are equal, a message is printed indicating that the inputNum is a strong integer. Otherwise, the software prints a warning indicating that the inputNum is not a strong number if the sum and inputNum are not equal. In the example code, the inputNum is set to 40585, which is a reliable value. As a result, the software will output to the console that “40585 is a strong number.”


40585 is a strong number

Also, learn about Emirp Number in Java here!

Find all the strong numbers in a given range

import java.util.*;   
import java.util.Scanner;

class RangeStrong_Number
	public static void main(String[] args)
	    int n1,n2;  
        // create scanner class object   
        Scanner sc =  new Scanner(;  
        System.out.print("Enter lower range : ");  
        n1 = sc.nextInt();  
        System.out.print("Enter upper range : ");  
        n2 = sc.nextInt(); 
		for (int i = n1; i <= n2; i++)
			if (isItStrong(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;

Explanation of the code:

Strong numbers are those that are equal to the factorial sum of their digits. The user is first asked to enter the lower and upper range of numbers by the application. The variables n1 and n2 respectively, hold these values.

Each integer in the specified range is iterated using a for loop inside the main procedure. The isItStrong method is called for each number to determine whether it is a strong number. The number is printed to the console if it is.

When given an integer as input, the isItStrong method determines whether the number is a strong one. The factorial of each number digit is calculated using the fact technique utilizing a while loop to extract each digit from the integer. A comparison between the original number and the factorial total is performed. They must be equal in order for the procedure to return true, which indicates that the number is a strong number.

Using a for loop, the fact technique determines the factorial of a given digit. In general, the application lets the user enter a range of numbers, checks those numbers for strong ones, and prints those to the console.


Enter lower range : 1
Enter upper range : 1000000

Difference between Strong numbers and other types of numbers

Strong numbers are unique in their characteristics and differ from other types of numbers in several ways. Here are the key differences:

Strong Numbers vs Prime Numbers:Prime numbers are only divisible by 1 and themselves, whereas Strong numbers are not restricted by divisibility rules.Strong numbers are based on the sum of the factorial of their digits, while prime numbers are focused on divisibility.
Strong Numbers vs Perfect Numbers:Perfect numbers are positive integers that are equal to the sum of their proper divisors, excluding the number itself.
Perfect numbers have a well-defined mathematical property.
On the other hand, Strong numbers are based on the sum of the factorials of their digits. Strong numbers use factorials.
Strong Numbers vs Armstrong Numbers:Armstrong numbers are those whose sum of the cubes of their digits is equal to the original number.
The calculation process for Armstrong numbers involves raising each digit to the power of the total number of digits
Armstrong numbers are a type of Strong numbers that have unique properties related to their digit manipulation.
Palindrome numbers remain unchanged when their digits are reversed. Palindrome numbers are often characterized by symmetry
Strong Numbers vs Palindrome Numbers:Strong numbers have a distinct calculation process based on the factorial of each digit. Strong numbers focus on the properties of factorials.Strong numbers have a distinct calculation process based on the factorial of each digit.Strong numbers focus on the properties of factorials.

Check out our blog on Armstrong Number in Java, here!

Enhancements and Advanced Concepts

A. Discussing possible optimizations to the Strong Number program

The Strong number program can be optimized for better performance and efficiency.

Memoization: Utilize memoization to store the factorial values of already calculated numbers. By caching the factorial values, the program can avoid recalculating them when checking for strong numbers.

Upper limit optimization: Optimize the program to only check numbers up to the highest input value, reducing computation time and improving the execution speed.

Learn more about the Palindrome Program in Java here!

B. Exploring additional features and functionalities

Consider including the following features and functionalities to further improve the Strong Number program:

User-defined input: Users should be able to enter a range and find all Strong numbers inside that range. The program would become more adaptable and user-friendly as a result.

Counting Strong Numbers: To count all Strong numbers within a specified range, modify the program. The frequency of Strong numbers within a given range would be made known to users thanks to this feature, which would offer them useful information.

Strong number sequence: Make a function that produces a series of Strong numbers up to a predetermined limit. To do this, iterate through the sequence of numbers, determine whether they are Strong numbers, and then add them. 

C. Introducing related topics like Special numbers or Armstrong numbers

In addition to Strong numbers, there are other interesting number concepts worth exploring. Consider introducing the following topics to expand the reader’s understanding:

Special numbers: Perfect, amicable, and triangular numbers are just a few examples of the various special number types. Describe the characteristics that make them special and how to recognize and calculate them. 

Armstrong numbers: Explain Armstrong numbers, also called Narcissistic numbers or the sum of their own digits raised to the power of the number of digits. Describe Armstrong numbers’ properties and uses in number theory and cryptography.

Throughout this blog, we have learned how to implement Strong numbers in Java. We have discussed the basic concepts of factorials, provided a step-by-step guide to identifying Strong numbers, and provided a Java program for reference. Following the outlined process and understanding the implementation steps, readers can now create their own Java program to identify Strong numbers accurately and efficiently.

In conclusion, Strong Numbers challenges mathematics and programming, providing valuable knowledge and skills in Java. I encourage everyone to apply this knowledge to practical programming tasks, further explore related topics, and continue their learning journey in the vast world of programming. Keep checking on our  Newtum’s website to explore our online coding courses covering Java, Python, PHP, and more.

About The Author

Leave a Reply