Published: 20 Aug 2025 | Reading Time: 7 min read
In the world of programming, numbers often hide interesting patterns, and one such fascinating concept is a strong number. A strong number in Java is a number whose value is equal to the sum of the 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.
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 digits equals the number itself. Factorial, marked as n!, is the product of all positive integers from 1 to n.
For instance, 145 is a Strong Number because:
1! + 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.
For a given number N with d digits a₁, a₂, …, aₐ, the number N is called a Strong Number if:
N = a₁! + a₂! + ⋯ + aₐ!
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.
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.
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:
Number: 40585
This approach can be implemented using a loop in Java to extract digits and using a function for calculating the factorial of a number.
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.");
}
}
}
Enter a number: 145
145 is a Strong Number.
This program takes an integer as input and checks if it is a strong number. The main logic revolves around calculating the factorial of each digit using the factorial 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.
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.
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.
Here, Java has a code to check a strong number using a simple iteration. Using this approach, each digit of the number is extracted, its factorial is determined, and the digits are then added.
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.");
}
}
}
Enter a number: 145
145 is a Strong Number.
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.
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.
Instead of loops, we can calculate the factorial using a recursive function. This approach is smart and displays the power of recursion.
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.");
}
}
}
Enter a number: 145
145 is a Strong Number.
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.
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).
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.
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.");
}
}
}
Enter a number: 145
145 is a Strong Number.
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.
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.
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");
}
}
}
40585 is a Strong Number
Time Complexity: O(n × m), where n = number of digits, m = value of each digit (for factorial calculation)
Space Complexity: O(1)
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");
}
}
}
40585 is a Strong Number
Time Complexity: O(n), where n = number of digits, factorial lookup is O(1)
Space Complexity: O(1). An array of size 10 stores factorials.
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.
This article has covered:
By mastering these concepts, programmers can enhance their logical thinking, improve their understanding of looping constructs, and develop efficient solutions for mathematical problems 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.
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.
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.
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.
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.
The common methods include simple iteration, recursive functions, and dynamic programming approaches where factorials are precomputed.
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.
Source: NxtWave - CCBP Blog
Original URL: https://www.ccbp.in/blog/articles/strong-number-in-java