C++ Strong Number Program

Beginner
5 min read
Updated: Oct 2026
3 programs
Live preview

What Is a Strong Number?

A strong number (also called a digital factorial number) equals the sum of the factorials of its digits.

Remember
n is strong  ↔  n = sum of d! for each digit d of n

Example: 145
  1! + 4! + 5! = 1 + 24 + 120 = 145  → strong

Classic values: 1, 2, 145, 40585
nDigit factorialsResult
1451 + 24 + 120Strong
11! = 1Strong
101! + 0! = 2Not strong
405854!+0!+5!+8!+5!Strong

How to Check a Strong Number in C++

Four clear steps — a digit-factorial lookup makes the check fast and clear.

  1. Precompute 0! through 9! in a small array (digits never need more).
  2. Copy n, then peel digits with n % 10.
  3. Add fact[digit] for each digit.
  4. Compare the total with the original number.
MethodIdeaTimeBest for
Lookup + digit loopAdd fact[d] for each digitO(digits)Interviews, single checks
Range scanReuse the helper for each valueO(N log N)Listing strong numbers
Trace termsPrint each d! contributionO(digits)Debugging / teaching

Pseudocode

Pseudocode
fact = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]

function IsStrong(n):
    if n <= 0:
        return false          // beginner rule: positive only
    original = n
    total = 0
    while n > 0:
        digit = n mod 10
        total = total + fact[digit]
        n = n / 10
    return total == original

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Definitionn == sum of digit factorials
Lookup0!..9! = 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880
Peel digitdigit = n % 10, then n /= 10
Trivial yes1, 2
Famous145, 40585
vs ArmstrongFactorials of digits, not powers
1..200Only 1, 2, 145

Live Preview

Enter a positive integer to see each digit factorial and the strong/not verdict.

Positive whole numbers. Tap a chip or type a value.

Result
n = 145
1! + 4! + 5! = 1 + 24 + 120 = 145
Verdict: STRONG

Worked Walkthrough — n = 145

Peel digits right to left, add factorials, then compare.

Calculation
n = 145

Digit 5: 5! = 120   total = 120
Digit 4: 4! = 24    total = 144
Digit 1: 1! = 1     total = 145

145 == 145  → strong

Counter-example: 10
  0! + 1! = 1 + 1 = 2  ≠ 10  → not strong

C++ Programs

Start with a single-value check using a factorial lookup. Then list strong numbers in a range, and finish with a term-by-term trace.

Example 1 — Check One Number

Precompute 0!..9!, sum digit factorials, and compare with n.

C++
#include <iostream>
using namespace std;

bool isStrongNumber(int n)
{
    int fact[] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880 };
    int original = n;
    int total = 0;

    while (n > 0)
    {
        int digit = n % 10;
        total += fact[digit];
        n /= 10;
    }

    return total == original;
}

int main()
{
    int number = 145;
    if (isStrongNumber(number))
    {
        cout << number << " is a Strong Number." << endl;
    }
    else
    {
        cout << number << " is not a Strong Number." << endl;
    }
    return 0;
}

How It Works

  1. The fact array stores every digit factorial once.
  2. Save original because the loop destroys n.
  3. Each step takes n % 10 and adds fact[digit].
  4. For 145: 120 + 24 + 1 = 145, so the method returns true.
  5. Never recompute factorial from scratch inside the digit loop.

Example 2 — Strong Numbers from 1 to 200

Reuse the helper to print every strong value in a small band.

C++
#include <iostream>
using namespace std;

bool isStrongNumber(int n)
{
    int fact[] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880 };
    int original = n;
    int total = 0;

    while (n > 0)
    {
        int digit = n % 10;
        total += fact[digit];
        n /= 10;
    }

    return total == original;
}

int main()
{
    cout << "Strong Numbers in the Range 1 to 200:" << endl;
    for (int i = 1; i <= 200; i++)
    {
        if (isStrongNumber(i))
        {
            cout << i << " ";
        }
    }
    cout << endl;
    return 0;
}

How It Works

  1. Loop i from 1 to 200 inclusive.
  2. Call isStrongNumber(i) for each value.
  3. Print matches only: 1, 2, and 145.
  4. The next famous strong number, 40585, is far above 200.
  5. This is the usual interview follow-up after a single check.

Example 3 — Trace Digit Factorials

Print each d! term so you can see why a candidate passes or fails.

C++
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

const int FACT[] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880 };

int main()
{
    int values[] = { 2, 10, 145, 99 };
    int count = sizeof(values) / sizeof(values[0]);

    for (int i = 0; i < count; i++)
    {
        int n = values[i];
        vector<string> terms;
        int total = 0;
        int x = n;

        while (x > 0)
        {
            int d = x % 10;
            total += FACT[d];
            terms.push_back(to_string(d) + "!=" + to_string(FACT[d]));
            x /= 10;
        }

        reverse(terms.begin(), terms.end());

        cout << n << ": ";
        for (size_t t = 0; t < terms.size(); t++)
        {
            if (t > 0)
            {
                cout << " + ";
            }
            cout << terms[t];
        }
        cout << " = " << total
             << " -> " << (total == n ? "strong" : "not strong")
             << endl;
    }
    return 0;
}

How It Works

  1. Digits are collected from right to left, then reversed for readable left-to-right order.
  2. Each term shows d! and its value from the lookup table.
  3. 10 fails because 1 + 1 = 2, not 10.
  4. 99 fails because 9! alone is already huge.
  5. Use this style when explaining the check on a whiteboard.

Edge Cases & Pitfalls

Check these before calling the solution done.

1 and 2

Trivial strong numbers

Both count. Do not special-case them away.

Zero

Usually excluded

Beginner checks focus on positive n. Note that 0! = 1.

Lookup

Precompute 0!..9!

Digits never exceed 9. Recomputing factorial in a nested loop is wasted work.

Large digits

9! grows fast

Numbers with many 9s blow past themselves quickly — useful for early exit if the sum exceeds n.

Time and Space Complexity

ProgramTimeExtra space
Single checkO(digits) = O(log n)O(1)
Range of size NO(N log N)O(1)
Trace termsO(digits)O(digits) for the term list

The factorial table is fixed size (10 entries), so it does not grow with n.

Key Takeaways

  • Definition: n equals the sum of factorials of its digits.
  • Lookup: store 0!..9! once, then index by digit.
  • Classic: 145 = 1! + 4! + 5!.
  • Test set: 1, 2, 145 strong; 10 and 99 not; 40585 strong.

One line: sum each digit’s factorial — if the total equals n, it is strong.

Frequently Asked Questions

A strong number equals the sum of the factorials of its digits. Example: 145 = 1! + 4! + 5! = 1 + 24 + 120.
Yes. 1! = 1 and 2! = 2, so both equal the sum of their digit factorials.
Precompute 0! through 9!, peel digits with n % 10, add fact[digit], then compare the total with the original number.
No. Armstrong numbers use powers of digits. Strong numbers use factorials of digits.
Checking one number is O(d) where d is the digit count (O(log n)). A range scan of size N is O(N log N).

Next: Sum of Array

Add every element in an array with a simple loop.

Sum of array tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful