Check Strong Number in JavaScript

Beginner
⏱️ 11 min read
📚 Updated: May 2026
🎯 2 Code Examples
Digit math

What you’ll learn

  • What a strong number (digital factorial) means.
  • How to compute factorials of digits efficiently using a small lookup table.
  • Two complete JavaScript programs: check one number and list strong numbers from 1 to 200.

Overview

A strong number is a positive integer that equals the sum of the factorials of its digits. Example: 145 is strong because 1! + 4! + 5! = 145. This page shows a clean C implementation and a small range listing.

Two programs

Example 1 checks 145. Example 2 prints all strong numbers in 1–200 (output: 1 2 145).

Live preview

Type a number and see the factorial addition (like 1! + 4! + 5!) and the verdict.

Simple optimization

Precompute 0! to 9! once, then each digit factorial is just an array lookup.

Prerequisites

Digit extraction with % and integer division, plus basic functions.

  • for/while loops and printing output with console.log.
  • Understanding n % 10 (last digit) and n / 10 (remove last digit).

What is a strong number?

A strong number (also called a digital factorial) is a number where:

number = sum of factorials of its digits

For 145, the digits are 1, 4, 5. Their factorials are 1! = 1, 4! = 24, 5! = 120. Adding them gives 145 again.

Worked example: 145

1! + 4! + 5! = 1 + 24 + 120 = 145 — so 145 is a strong number.

Live preview

Type a positive integer and see the factorial sum of its digits. (This demo caps inputs for speed.)

Use whole numbers n ≥ 1. This preview allows up to 1,000,000,000.

Live result
Press “Run check” to see the factorial sum and verdict.

Algorithm

Goal: compute the sum of digit! for each digit of n and compare to n.

Precompute factorials of digits

Store 0! through 9! in an array fact[10]. This is tiny and avoids calling factorial repeatedly.

Extract digits and add

While a working copy x is greater than 0: take digit = x % 10, add fact[digit] to sum, then do x /= 10.

Compare

If sum == n, the number is strong; otherwise it is not.

📜 Pseudocode

Pseudocode
fact[0..9] = {1,1,2,6,24,120,720,5040,40320,362880}

function isStrong(n):
    sum ← 0
    x ← n
    while x > 0:
        digit ← x mod 10
        sum ← sum + fact[digit]
        x ← floor(x / 10)
    return sum = n
1

Check a single number

Uses a digit-factorial lookup table instead of recursion. Sample input is 145.

JavaScript
function isStrongNumber(n) {
  const fact = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880];
  const original = n;
  let sum = 0;

  while (n > 0) {
    const digit = n % 10;
    sum += fact[digit];
    n = Math.floor(n / 10);
  }

  return sum === original;
}

const number = 145;
if (isStrongNumber(number)) {
  console.log(number + " is a Strong Number.");
} else {
  console.log(number + " is not a Strong Number.");
}
Try it Yourself
2

Strong numbers from 1 to 200

Loops through the range and prints values that satisfy the same check. Output is 1 2 145.

JavaScript
function isStrongNumber(n) {
  const fact = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880];
  const original = n;
  let sum = 0;

  while (n > 0) {
    const digit = n % 10;
    sum += fact[digit];
    n = Math.floor(n / 10);
  }

  return sum === original;
}

const values = [];
for (let i = 1; i <= 200; i++) {
  if (isStrongNumber(i)) {
    values.push(String(i));
  }
}

console.log("Strong Numbers in the Range 1 to 200:");
console.log(values.join(" "));
Try it Yourself

Optimization

Lookup table. The biggest win is precomputing 0! through 9!. After that, each digit contributes in O(1) time.

Short-circuit. If the running sum already exceeds the original number, you can stop early (optional).

Interview: mention the lookup table and that digit count is only O(log n).

❓ FAQ

Take each digit of the number, compute the factorial of that digit, and add them. If the total equals the original number, it is a strong number. Example: 145 is strong because 1! + 4! + 5! = 1 + 24 + 120 = 145.
Yes. 1! = 1 and 2! = 2, so both match the original number.
It depends on the convention. Since 0! = 1, most definitions treat 0 as not strong (the sum would be 1). This page focuses on positive integers like 1, 2, 145.
A number's digits are only 0 through 9. Precomputing 0! to 9! in an array makes each check faster and simpler: you just look up factorial[digit] instead of recomputing factorial repeatedly.
They are 1, 2, and 145.
No. Armstrong numbers use powers of digits (like cubes), while strong numbers use factorials of digits.

🔄 Input / output examples

nStrong?Reason
145Yes1! + 4! + 5! = 145
2Yes2! = 2
10No1! + 0! = 2
99No9! + 9! = 725760

Edge cases and pitfalls

Most issues come from handling 0 and from recomputing factorials too often.

n = 0

What should happen?

If you include 0, its digit list is “0”, so the sum is 0! = 1. That means 0 is not strong under the usual “sum equals number” rule. Many beginner problems start at 1 anyway.

Performance

Avoid recursion per digit

A recursive factorial function works but repeats the same work for each digit. The array fact[10] is simpler and faster.

Overflow

Factorials fit comfortably

The largest digit factorial is 9! = 362880, so summing a handful of digits fits in int for normal input sizes. For extreme digit counts, use a wider type.

⏱️ Time and space complexity

TaskTimeExtra space
Check one nO(d) where d is number of digitsO(1)
Scan 1..UO(U log U) (digit work per number)O(1)

Summary

  • Definition: strong means the sum of digit! equals the number.
  • Code: use a fact[10] lookup table and extract digits with % 10.
  • Range 1..200: 1 2 145.
Did you know?

Strong numbers are also called digital factorial numbers. In base 10, the classic examples are 1, 2, 145, and 40585 (because each equals the sum of factorials of its digits).

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.

9 people found this page helpful