C Example Code2026-03-252 min read
Learn how to check if a number is a Strong (Krishnamurthy) number in C by calculating the sum of factorials of digits with complete code.
What is a Strong (Krishnamurthy) Number?
A Strong Number (also known as a Krishnamurthy Number or Peterson Number) is a special integer whose sum of the factorials of its digits equals the original number.
Example:
For $n = 145$:
text (ISO Standard)1! + 4! + 5! = 1 + 24 + 120 = 145
Since the sum equals 145, 145 is a Strong Number.
Working C Code
c (ISO Standard)#include <stdio.h> #include <stdbool.h> /* Precomputed digit factorials (0! through 9!) */ static const int FACTORIALS[10] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880 }; bool isStrongNumber(int n) { if (n <= 0) return false; int original = n; int sum = 0; int temp = n; while (temp > 0) { int digit = temp % 10; sum += FACTORIALS[digit]; temp /= 10; } return sum == original; } int main() { int testCases[] = {1, 2, 145, 40585, 123, 500}; int count = sizeof(testCases) / sizeof(testCases[0]); printf("Strong Number Verification in C:\n\n"); for (int i = 0; i < count; i++) { int num = testCases[i]; if (isStrongNumber(num)) { printf(" %d -> STRONG NUMBER\n", num); } else { printf(" %d -> Not a strong number\n", num); } } return 0; }
Sample Output
text (ISO Standard)Strong Number Verification in C: 1 -> STRONG NUMBER 2 -> STRONG NUMBER 145 -> STRONG NUMBER 40585 -> STRONG NUMBER 123 -> Not a strong number 500 -> Not a strong number
Complexity Analysis
- Time Complexity:
O(log₁₀ n)digit extractions. - Space Complexity:
O(1)constant space using precomputed factorials.
Related Guides
- Review factorial loops in Factorial Program in C.
- Compare with digit power sums in Armstrong Number in C.