Summarise With AI
Back

Understanding Strong Number in Java

Summarise With Ai
20 Aug 2025
7 min read

In the world of programming, the number often hides in patterns, and one such interesting concept is a strong number. A strong number is a number whose value is equal to the sum of its factorials of its digits. 

Java, with its simplicity and rich set of features, makes it easy to implement checks for Strong Numbers. By dividing the number into digits, calculating factorials and their sum, developers can easily write programs that identify these unique numbers. Understanding a strong number is not just a fun exercise; It helps to focus logical thinking, looping constructs, and basic functions in Java.

In this article, we will explore what strong numbers are, how to determine if a number is strong using Java, and provide various implementations and examples.

What is a Strong Number?

A strong number is a special type of number in mathematics. It is defined as a number in which the sum of the factorials of its individual numbers equals the number itself. Factorial, marked N!, is the product of all positive integers from 1 to n.

For instance, 145 is a Strong Number because 

1!+4!+5!=1451! + 4! + 5! = 1451!+4!+5!=145. 

This property makes strong numbers unique and rare among integers.

Strong numbers show a relationship between a number's digits and the factorial concept. The concept can be extended to identify strong numbers within a specific field or check if a given number satisfies the measures.

Mathematical Representation

For a given number N with ddd digits a1,a2,…,ad, the number N is called a Strong Number if:

N=a1!+a2!+⋯+ad!

Example of a Strong Number

If N = 145, the digits are 1, 4, and 5. Calculating the factorial of each digit:

1!+4!+5!=1+24+120=145

Since the sum equals the original number, N = 145 is a Strong Number.

Example of a Non-Strong Number

Consider N=123, with digits 1, 2, and 3. Calculating the factorial of each digit:

1!+2!+3!=1+2+6=9

Here, the sum (9) does not equal the original number (123), so N=123 is not a Strong Number.

Algorithm to Check a Strong Number in Java

A strong number is a number in which the sum of the factorials of its digits equals the number itself. 

For example, 40585 is a Strong Number because 

4!+0!+5!+8!+5!=40585.

Here’s how you can check it step by step:

  1. Take the given number and store it in a temporary variable.
  2. Extract each digit of the number one by one.
  3. Calculate the factorial of each digit.
  4. Add all the factorials together.
  5. Compare the sum with the original number.
    • If they are equal, the number is a Strong Number.
    • If not, it is not a Strong Number.

Example:

  • Number: 40585
    • Digits: 4, 0, 5, 8, 5
    • Factorials: 4! = 24, 0! = 1, 5! = 120, 8! = 40320, 5! = 120
    • Sum: 24 + 1 + 120 + 40320 + 120 = 40585 → Strong Number

This approach can be implemented using a loop in Java to extract digits and using a function for calculating the factorial of a number.

Strong Number Program in Java

In this simple Java implementation of a strong number, we determine whether or not the number satisfies the requirement that it be a strong number:

import java.util.Scanner;

public class StrongNumber {
    // Method to calculate factorial
    public static long factorial(int n) {
        long fact = 1;
        for (int i = 1; i <= n; i++) {
            fact *= i;
        }
        return fact;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = scanner.nextInt();
        int originalNum = num;
        int sum = 0;

        // Calculate sum of factorials of each digit
        while (num > 0) {
            int digit = num % 10;
            sum += factorial(digit);
            num /= 10;
        }

        // Check if it is a strong number
        if (sum == originalNum) {
            System.out.println(originalNum + " is a Strong Number.");
        } else {
            System.out.println(originalNum + " is not a Strong Number.");
        }
    }
}

Output 

Enter a number: 145

145 is a Strong Number.

Explanation of the Code

This program takes an integer as input and checks if it is a strong number. The main argument revolves around calculating the factorial of each digit using the factory method, adding these factors together and comparing the result with the original number. 

The loop iterates through each digit of the number by repeatedly taking the remainder when divided by 10 to reduce the number. If the sum of the factorials of all digits matches the original number, the number is said to be a Strong Number; otherwise, it is not. 

Complexity of the Code

Time Complexity: To calculate the factorial for each digit takes O(d), where d is the digit's value. For a number with n digits, the loop runs n times. Then, the overall time complexity is approximately O(n⋅d), where d is the average value of the digits. For practical purposes, d is small, so the complexity simplifies to O(n).

Space Complexity: The program uses a few variables (num, sum, digit) and the recursive stack for factorial calculations. Space complexity is O(1) as no additional data structures are used.

Methods to Implement Strong Number Program in Java

There are many methods to implement a strong number in Java program. Here are more advanced methods to implement a Strong Number in Java, which can improve efficiency and readability while handling larger inputs or complex designs.

Method 1: Using Simple Iteration

Here, Java has a code to check a strong number using a simple recurrence. Using this approach, each digit of the number is extracted, its factorial is determined, and the digits are then added.

Strong Number Java Program using Simple Iteration

import java.util.Scanner;

public class StrongNumber {
    // Method to calculate factorial
    public static long factorial(int n) {
        long fact = 1;
        for (int i = 1; i <= n; i++) {
            fact *= i;
        }
        return fact;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = scanner.nextInt();
        int originalNum = num;
        int sum = 0;

        // Calculate the sum of factorials of each digit
        while (num > 0) {
            int digit = num % 10;
            sum += factorial(digit);
            num /= 10;
        }

        // Check if it is a strong number
        if (sum == originalNum) {
            System.out.println(originalNum + " is a Strong Number.");
        } else {
            System.out.println(originalNum + " is not a Strong Number.");
        }
    }
}

Output

Enter a number: 145
145 is a Strong Number.‍

Explanation of the Code

The user is prompted to enter a number, and the program stores the original number for comparison. It then iterates through the digits of the number by taking the remainder when divided by 10. 

For each digit, the program computes the factorial using the factorial method, which calculates the sum of these factorials. Once the iteration is complete, the program compares the sum with the original number to determine if it is a Strong Number.

Complexity of the Code

Time Complexity: For each digit, the program computes its factorial, which takes O(1) time since the maximum digit is 9. The main loop runs once for each digit, so the overall time complexity is O(d), where d is the number of digits in the number.

Space Complexity: The space complexity is O(1) because the program uses only a few variables to store the number, its sum, and the digits.

Method 2: Using Recursive Function

Instead of loops, we can calculate the factorial using a recursive function. This approach is smart and displays the power of recursion.

Strong Number Java Program using a Recursive Function

import java.util.Scanner;

public class RecursiveStrongNumber {

    // Recursive method to calculate factorial
    public static long factorial(int n) {
        if (n == 0 || n == 1) {
            return 1;
        }
        return n * factorial(n - 1);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = scanner.nextInt();
        int originalNum = num;
        int sum = 0;

        // Calculate sum of factorials of each digit
        while (num > 0) {
            int digit = num % 10;
            sum += factorial(digit);
            num /= 10;
        }

        // Check if it is a strong number
        if (sum == originalNum) {
            System.out.println(originalNum + " is a Strong Number.");
        } else {
            System.out.println(originalNum + " is not a Strong Number.");
        }
    }
}

Output

Enter a number: 145
145 is a Strong Number.

Explanation of the Code

The factorial method checks if the number is either 0 or 1 (in which case it returns 1), or it recursively calculates the factorial by multiplying the number by the factorial of the previous number.

The program iterates through the digits of the number, calculates the factorial of each, and adds them to a cumulative sum. Finally, it compares the sum of the factorials with the original number to determine if it is a Strong Number.

Complexity of the Code

Time Complexity: The recursive factorial method takes O(d) time, where d is the value of the digit being processed. The main loop runs once for each digit in the number, so the overall time complexity is O(n).

Space Complexity: The space complexity for the recursive factorial method is O(d) due to the recursion stack, where d is the digit value being processed (max 9). Overall, the space complexity is O(1).

Method 3: Smart Dynamic Programming Approach

Dynamic programming can be used to precompute and store the factorial values for digits. This reduces repetitive calculations and improves efficiency, which is mainly for large numbers.

Strong Number Java Program using Smart Dynamic Programming Approach

import java.util.Scanner;

public class DPSmartStrongNumber {
    static int[] factorials = new int[10];

    static {
        factorials[0] = factorials[1] = 1;
        for (int i = 2; i < factorials.length; i++) {
            factorials[i] = i * factorials[i - 1];
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = scanner.nextInt();
        int originalNum = num;
        int sum = 0;

        // Calculate sum of precomputed factorials
        while (num > 0) {
            int digit = num % 10;
            sum += factorials[digit];
            num /= 10;
        }

        // Check if it is a strong number
        if (sum == originalNum) {
            System.out.println(originalNum + " is a Strong Number.");
        } else {
            System.out.println(originalNum + " is not a Strong Number.");
        }
    }
}

Output

Enter a number: 145
145 is a Strong Number.

Explanation of the Code

The factorial values are stored in a static array, factorials, which are initialized before the main logic. This avoids repeated calculations of the same factorials during the digit-by-digit processing. 

In the main method, the program iterates through the digits of the input number, accessing the precomputed factorial for each digit and summing these values. Finally, the program compares the sum of the factorials with the original number to determine if it is a Strong Number.

Complexity of the Code

Time Complexity: The overall time complexity is O(d), where d represents the number of digits in the input number.

Space Complexity: The space complexity is O(1) for the main logic since the precomputed factorial array of size 10 is used, and the program only requires a few variables to store the sum and input values.

Examples of Strong Number Java Programs

Strong Number Program in Java Using String Conversion

This method converts the number into a string. Each digit is processed individually to calculate its factorial. The sum of all factorials is then compared with the original number to check if it is a Strong Number.

public class StrongNumberStringExample {
    public static int getFactorial(int n) {
        int result = 1;
        for (int i = 1; i <= n; i++) {
            result *= i;
        }
        return result;
    }

    public static void main(String[] args) {
        int num = 40585;
        int total = 0;

        String strNum = Integer.toString(num);
        for (int i = 0; i < strNum.length(); i++) {
            int digit = strNum.charAt(i) - '0';
            total += getFactorial(digit);
        }

        if (total == num) {
            System.out.println(num + " is a Strong Number");
        } else {
            System.out.println(num + " is not a Strong Number");
        }
    }
}

Output

40585 is a Strong Number

Explanation:

  • The number 40585 is converted to a string "40585".
  • Each digit is extracted and its factorial is calculated: 4! = 24, 0! = 1, 5! = 120, 8! = 40320, 5! = 120.
  • The sum of factorials: 24 + 1 + 120 + 40320 + 120 = 40585.
  • Since the sum equals the original number, it is a Strong Number.

Complexity of the Code

Time Complexity: O(n * m), n = number of digits, m = value of each digit (for factorial calculation)

Space Complexity: O(1)

Strong Number Program in Java Using Precomputed Factorials

This method precomputes the factorials of digits 0–9 in an array, reducing repeated calculations. Each digit of the number is used to look up the factorial from the array, then summed and compared to the original number.

public class StrongNumberPrecomputeExample {
    public static void main(String[] args) {
        int numberToCheck = 40585;
        int[] factorialTable = new int[10];
        factorialTable[0] = 1;

        // Precompute factorials from 1! to 9!
        for (int i = 1; i < 10; i++) {
            factorialTable[i] = factorialTable[i - 1] * i;
        }

        int sumOfFactorials = 0;
        int tempNumber = numberToCheck;

        while (tempNumber > 0) {
            int digit = tempNumber % 10;
            sumOfFactorials += factorialTable[digit];
            tempNumber /= 10;
        }

        if (sumOfFactorials == numberToCheck) {
            System.out.println(numberToCheck + " is a Strong Number");
        } else {
            System.out.println(numberToCheck + " is not a Strong Number");
        }
    }
}

Output:

40585 is a Strong Number

Explanation:

  • Factorials for digits 0–9 are precomputed and stored in an array.

  • Each digit of the number is extracted using % and / operators.
  • The factorial of each digit is added from the precomputed array.
  • If the total equals the original number, it is a Strong Number.
  • This method avoids repeated factorial calculations.

Complexity of the Code

Time Complexity is O(n), n = number of digits, factorial lookup is O(1)

Space Complexity is O(1). An array of size 10 stores factorials.

Conclusion

Understanding and identifying strong numbers in Java provides a challenge for both novice and experienced programmers. A strong number is defined as a number where the sum of the factorials of its digits equals the number itself, a concept that not only interests but also introduces various techniques to solve the problem in Java.

Frequently Asked Questions

1. Which is a strong number in Java?

A strong number in Java is a number where the sum of the factorials of its digits is equal to the number itself. For example, 145 is a strong number because 1! + 4! + 5! equals 145.

2. What is the time complexity of checking a strong number in Java?

The time complexity for checking a strong number in Java depends on the number of digits in the input. Normally, it is O(n), where n is the number of digits.

3. How is 145 a special number?

The number 145 is considered special because it satisfies the condition of a strong number. The sum of the factorials of its digits (1! + 4! + 5!) is equal to the number itself, 145. This makes 145 a unique number.

4. Is 123 a strong number?

No, 123 is not a strong number. To check, we calculate the sum of the factorials of its digits: 1! = 1, 2! = 2, and 3! = 6. The sum of these values is 1 + 2 + 6 = 9, which does not equal 123. Hence, 123 does not meet the criteria of a strong number, making it a non-strong number.

5. How can I check if a number is strong in Java?

To check if a number is strong in Java, first, you need to extract each digit of the number. Then, calculate the factorial of each digit and sum them. Finally, compare the sum with the original number. If the sum matches the number, it is a strong number. Otherwise, it is not. 

6. What are the most common methods to implement strong number checks in Java?

The common methods include simple iteration, recursive functions, and dynamic programming approaches where factorials are precomputed.

7. Can a large number be a strong number in Java?

Yes, a large number can be a strong number in Java, but it is less likely due to the rarity of such numbers. The concept of a strong number is independent of size, so as long as the sum of the factorials of the digits equals the number itself, it qualifies as a strong number.

Summarise With Ai

Read More Articles

Chat with us
Chat with us
Talk to career expert