Strong Number in C – Check Using Digit Factorials

A strong number in C (also called a special number or Peterson number) is a number where the sum of the factorials of its individual digits equals the number itself. For 145: the digits are 1, 4, and 5. Their factorials are 1! = 1, 4! = 24, 5! = 120. The sum 1 + 24 + 120 = 145, so 145 is a strong number. There are only four strong numbers in total: 1, 2, 145, and 40585.

How It Works — Step by Step

Check if 145 is a strong number:

Digit Factorial Running sum
5 (units) 5! = 120 120
4 (tens) 4! = 24 144
1 (hundreds) 1! = 1 145

Sum = 145 = original number → Strong number ✓

Check if 40585 is a strong number:

Digit Factorial
5 5! = 120
8 8! = 40320
5 5! = 120
0 0! = 1
4 4! = 24
Sum 120 + 40320 + 120 + 1 + 24 = 40585 ✓

Factorial Reference Table (digits 0–9)

Digit Factorial
0 0! = 1
1 1! = 1
2 2! = 2
3 3! = 6
4 4! = 24
5 5! = 120
6 6! = 720
7 7! = 5040
8 8! = 40320
9 9! = 362880

C Program for Strong Number

/* Strong number check in C
 * Compile: gcc -ansi -Wall -Wextra strong.c -o strong */
#include <stdio.h>

int factorial(int n)
{
    int result = 1, i;
    for (i = 2; i <= n; i++)
        result *= i;
    return result;
}

int is_strong(int n)
{
    int temp = n, sum = 0, digit;
    while (temp > 0) {
        digit = temp % 10;
        sum += factorial(digit);
        temp /= 10;
    }
    return sum == n;
}

int main(void)
{
    int num;

    printf("Enter a positive integer: ");
    scanf("%d", &num);

    if (num <= 0) {
        printf("Please enter a positive integer.\n");
        return 1;
    }

    if (is_strong(num))
        printf("%d is a strong number.\n", num);
    else
        printf("%d is not a strong number.\n", num);

    return 0;
}

How to Compile and Run

gcc -ansi -Wall -Wextra strong.c -o strong
./strong

Sample Input and Output

Enter a positive integer: 145
145 is a strong number.
Enter a positive integer: 40585
40585 is a strong number.
Enter a positive integer: 1
1 is a strong number.
Enter a positive integer: 100
100 is not a strong number.

Code Explanation

  • factorial(n) — computes n! using a for loop starting at 2 (multiplying by 1 changes nothing, so the loop skips it). For n = 0 or n = 1, the loop does not execute and returns the initial value 1, which is correct since 0! = 1! = 1.
  • digit = temp % 10 — extracts the rightmost digit. For 145: first call gives 5, then 4, then 1. After each extraction, temp /= 10 drops that digit.
  • sum += factorial(digit) — since digits are always 0–9, factorial() computes at most 9! = 362880, well within int range.
  • return sum == n — compares the accumulated factorial sum against the original number. Returns 1 (true) if they match.
  • Why temp not n — the original value n is preserved so it can be compared in is_strong(). temp is the working copy that gets consumed by the digit-extraction loop.

Why Only Four Strong Numbers Exist

As a number gets longer, the maximum possible digit factorial sum grows much more slowly than the number itself:

Digits Largest number Max factorial sum
1 9 9! = 362880 (exceeds 1-digit range)
2 99 9! + 9! = 725760
6 999999 6 × 9! = 2177280 < 1,000,000
7 9999999 7 × 9! = 2540160 < 10,000,000

From 8 digits onward, the maximum factorial sum can never reach the smallest 8-digit number. This proves there are finitely many strong numbers, and exhaustive search has confirmed there are exactly four: 1, 2, 145, 40585.

What This Program Teaches

  • Digit extraction patternn % 10 and n /= 10 is the standard loop for processing a number digit by digit, used in Armstrong numbers, digit sum, and digit reverse programs.
  • Separating logic into functionsfactorial() and is_strong() keep main() clean. Each function has a single, testable responsibility.
  • Reusing results — once factorial() is written for strong number, it can be reused directly in permutation/combination programs.
  • Why 0! = 1 — the loop initializes result to 1 and only multiplies for i ≥ 2, which automatically handles the 0! = 1 case without a special branch. This is mathematically correct: 0! is defined as 1 because it represents the empty product.

Related Programs

Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
 | 
C Programming: A Modern Approach — K.N. King (India) |
(US)

Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>