Strong Number Program: Check in C, Python, and Java
Understand strong numbers (Krishnamurthy numbers), verify 145 and 40585 step by step, and write clean check programs in C, Python, and Java.
A strong number (also called a Krishnamurthy number) is one where the sum of the factorials of its digits equals the number itself.
The two numbers placement tests use as canonical examples are 145 and 40585. Before writing a single line of code, verify both by hand. That verification step is what separates a student who understands the property from one who memorised the answer.
Verifying 145 and 40585
Checking 145
The digits of 145 are 1, 4, and 5. Compute the factorial of each:
1!= 14!= 4 × 3 × 2 × 1 = 245!= 5 × 4 × 3 × 2 × 1 = 120- Sum: 1 + 24 + 120 = 145
That matches the original number. 145 is a strong number.
Checking 40585
The digits of 40585 are 4, 0, 5, 8, and 5, read left to right. Their factorials:
4!= 240!= 1 (zero factorial equals one by definition)5!= 1208!= 8 × 7 × 6 × 5 × 4 × 3 × 2 × 1 = 403205!= 120- Sum: 24 + 1 + 120 + 40320 + 120 = 40585
Step-by-step: 24 + 1 = 25, then 25 + 120 = 145, then 145 + 40320 = 40465, then 40465 + 120 = 40585. Confirmed.
According to the OEIS sequence A014080, only four strong numbers exist across all positive integers: 1, 2, 145, and 40585. The digit-factorial sum grows far more slowly than the number itself once you pass a certain size, so no larger ones can satisfy the property.
How the Algorithm Works
The core logic is a digit-extraction loop. Every positive integer can be decomposed digit by digit using two operations:
- Take
n % 10to isolate the last (rightmost) digit. - Compute the factorial of that digit.
- Add the factorial to a running sum.
- Use
n / 10(integer division) to drop the last digit. - Repeat until
nreaches zero. - Compare the accumulated sum to the original value.
A traced run for n = 145, with the original stored in a separate variable:
- Start: temp = 145, sum = 0
- Step 1: digit =
145 % 10= 5, factorial = 120, sum = 120, temp =145 / 10= 14 - Step 2: digit =
14 % 10= 4, factorial = 24, sum = 144, temp =14 / 10= 1 - Step 3: digit =
1 % 10= 1, factorial = 1, sum = 145, temp =1 / 10= 0 - Final: sum (145) equals original (145), so the number is strong.
One optimisation worth knowing: the digits of any number are always 0 through 9. That means you only ever need to compute ten distinct factorial values. A range-search program that checks every number from 1 to 100000 would recompute the same factorials millions of times using the naive approach. Precomputing those ten values in a lookup array at startup reduces the work to a simple array lookup per digit, which is faster and easier to read. The ten values are: 0! = 1, 1! = 1, 2! = 2, 3! = 6, 4! = 24, 5! = 120, 6! = 720, 7! = 5040, 8! = 40320, 9! = 362880.
Program in C
The C implementation needs a factorial helper function and a digit-extraction loop. The isStrong function stores the original value in temp before the loop starts, so the final comparison can reference the untouched original. Note the factorial for digit 0: the loop inside factorial starts at i = 2, so for n = 0, the loop never executes and f stays at its initial value of 1, which correctly represents 0!.
#include <stdio.h>
int factorial(int n) {
int f = 1;
for (int i = 2; i <= n; i++)
f *= i;
return f;
}
int isStrong(int n) {
int temp = n, sum = 0;
while (temp > 0) {
sum += factorial(temp % 10);
temp /= 10;
}
return sum == n;
}
int main() {
int n;
scanf("%d", &n);
printf(isStrong(n) ? "Strong number\n" : "Not a strong number\n");
return 0;
}
Modifying n directly inside the loop would leave it at zero by the time you need the comparison. Keeping a separate temp is the standard pattern for any digit-decomposition problem.
Program in Python and Java
Python
Python’s standard library ships math.factorial, which reduces the check to a single expression. The str(n) trick converts the number to a string so you can iterate over each character-digit without writing a manual modulo loop.
import math
def is_strong(n):
return sum(math.factorial(int(d)) for d in str(n)) == n
n = int(input())
print("Strong number" if is_strong(n) else "Not a strong number")
The Python math.factorial documentation notes that the function raises a ValueError for negative inputs, so is_strong is safe for any non-negative integer you pass it. The string-conversion approach also handles multi-digit numbers correctly because Python iterates the string one character at a time, each character being a single decimal digit.
Java
Java does not have a built-in factorial function, so the helper is manual. Use long for the return type: the largest single digit is 9, and 9! = 362880, which fits in an int. But a multi-digit number where every digit is 9 could accumulate a sum that exceeds int range, so long is the safer choice for the accumulator.
public class StrongNumber {
static long factorial(int n) {
long f = 1;
for (int i = 2; i <= n; i++)
f *= i;
return f;
}
public static void main(String[] args) {
int n = 145;
int temp = n;
long sum = 0;
while (temp > 0) {
sum += factorial(temp % 10);
temp /= 10;
}
System.out.println(sum == n ? "Strong number" : "Not a strong number");
}
}
The sum == n comparison promotes n to long automatically in Java, so no explicit cast is needed.
Finding All Strong Numbers in a Range
Wrap the check in an outer loop to find every strong number below a limit:
for (int i = 1; i <= limit; i++) {
if (isStrong(i))
printf("%d\n", i);
}
Running this from 1 to 100000 outputs: 1, 2, 145, 40585. Those four are all that exist. The reason no larger strong numbers appear is mathematical: for a d-digit number, the maximum possible digit-factorial sum is d × 9! = d × 362880. For large d, the actual number grows as a power of 10 and outpaces this linear bound, so the equality between the number and its digit-factorial sum can never hold beyond 40585.
Knowing that answer (exactly four strong numbers, and the mathematical reason why) is the follow-up interviewers expect after you write the code.
Why Placement Rounds Use This
Placement coding tests at companies like TCS, Wipro, and Cognizant include digit-property programs because they test a compact, composable set of skills: loop control, modulo arithmetic, and a mathematical property applied per digit. The problems are simple enough to solve in 15 minutes but nuanced enough to reveal whether a student can handle edge cases under pressure.
The same digit-extraction loop structure appears across a family of related problems. Number-pattern programs and strong number checks both reduce to the same skeleton: extract digit, apply property, accumulate, advance. If you can write one cleanly and explain the trace, you can adapt the pattern to the others without re-deriving the loop from scratch.
Number boundary problems and digit-property checks test whether you can reason precisely about numeric ranges, which pairs directly with knowing why only four strong numbers exist. These are not isolated facts.
The edge case that trips students in timed tests is 0!. A program that initialises factorial to 0 instead of 1 silently produces wrong answers for any input containing the digit zero. 40585 is the standard catch: it contains a zero. If your code returns “Not a strong number” for 40585, the 0! initialisation is almost always the culprit.
For IT fresher roles that include a coding round, being able to trace through a digit-loop problem at the whiteboard, not just recall the output, is what interviewers are testing. Knowing that 40585 works because 0! = 1 is the answer to the follow-up question almost every interviewer who sets this problem has ready.
Mastering the digit-loop template is step one. Step two is adapting it when the property changes: digit-sum-of-squares, digit-product checks, or a custom modular property. TinkerLLM at ₹499 gives you an LLM sandbox where you can generate test cases, mutate the property definition, and verify your modified code without switching tools. The jump from “I can solve this specific problem” to “I can handle any digit-property variant on the fly” is what separates candidates who pass coding rounds consistently.
Primary sources
Frequently asked questions
Are 1 and 2 considered strong numbers?
Yes. 1 factorial equals 1 and 2 factorial equals 2, so both satisfy the definition. They are the trivial cases; placement tests focus on 145 and 40585.
What is the difference between a strong number and an Armstrong number?
Armstrong numbers raise each digit to the power of the digit count. Strong numbers sum the factorials of the digits. Both are digit-property checks but use different operations.
How many strong numbers exist?
Exactly four: 1, 2, 145, and 40585. The OEIS sequence A014080 confirms no others exist across all positive integers.
Is 0 a strong number?
0 factorial equals 1 by convention, so the digit-factorial sum for a single-digit 0 would be 1, not 0. Under the standard definition, 0 is not a strong number.
What does 0 factorial equal, and why does it matter for this check?
0 factorial equals 1 by mathematical convention. This matters when the input contains the digit 0, as in 40585, where the zero digit contributes 1 to the factorial sum, not 0.
A self-paced playground for building with LLMs.
TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.
Try TinkerLLM (₹499)