Check Strong Number in Java

Beginner
⏱️ 10 min read
📚 Updated: May 2026
🎯 2 Code Examples
Number theory

What you’ll learn

  • Strong number definition via digit factorial sums.
  • Fast digit factorial lookup for 0..9.
  • Single-value check and range listing in Java.
  • How to reason about complexity using number of digits.

Overview

A strong number equals the sum of factorials of its digits. Example: 145 = 1! + 4! + 5! = 145. Classic values are 1, 2, 145, 40585.

Two Java programs

Check one value and list strong numbers from 1 to 200.

Live preview

See factorial-expression sum and verdict instantly.

Simple optimization

Precompute factorials for digits 0 to 9 once.

Prerequisites

Digit extraction, factorial basics, and loop traversal of numbers.

  • Use % 10 and / 10 to process digits.
  • Precompute factorial values for digits 0 through 9.

What is a strong number?

If sum of factorials of all decimal digits equals the number itself, the number is strong.

This is also called a digital factorial number in many interview sets.

Quick examples

145Strong
Check
1! + 4! + 5! = 145
123Not
Sum
1!+2!+3! = 9
2Strong
Reason
2! = 2

Live preview

Type a positive integer to see the factorial sum of its digits and final verdict.

Use whole numbers n ≥ 1. Preview supports up to 1,000,000,000.

Live result
Press “Run check”.

Algorithm

Prepare factorial lookup

Store factorials for digits 0..9 once.

Extract digits

Use modulo/division to process each digit.

Compare sums

If factorial-digit sum equals original, it is strong.

📜 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 += fact[digit]
    x = floor(x / 10)
  return sum == n
1

Check one number

java
public class Main {
    static boolean isStrongNumber(int n) {
        int[] fact = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
        int original = n;
        int sum = 0;

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

    public static void main(String[] args) {
        int number = 145;
        if (isStrongNumber(number)) {
            System.out.println(number + " is a Strong Number.");
        } else {
            System.out.println(number + " is not a Strong Number.");
        }
    }
}
2

Strong numbers from 1 to 200

java
public class Main {
    static boolean isStrongNumber(int n) {
        int[] fact = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
        int original = n;
        int sum = 0;
        while (n > 0) {
            int digit = n % 10;
            sum += fact[digit];
            n /= 10;
        }
        return sum == original;
    }

    public static void main(String[] args) {
        System.out.println("Strong Numbers in the Range 1 to 200:");
        for (int i = 1; i <= 200; i++) {
            if (isStrongNumber(i)) {
                System.out.print(i + " ");
            }
        }
    }
}

Optimization

Lookup table. Precompute 0! through 9! once, then each digit contribution is O(1).

Short-circuit. Optional early stop when running sum already exceeds original.

Interview: mention lookup-table win and digit-count complexity.

❓ FAQ

A strong number equals the sum of factorials of its digits.
Yes. 1! + 4! + 5! = 1 + 24 + 120 = 145.
Yes, because 1! = 1 and 2! = 2.
Usually no for this lesson, since 0! = 1 and does not match 0.
Digits are only 0 to 9, so precomputing their factorials is fast and simple.
Checking one number takes O(d) where d is number of digits, with O(1) extra space.

Input/output examples

Input nOutput
145Strong number
123Not strong
2Strong number

Edge cases and pitfalls

Common issues are handling zero and recomputing factorial repeatedly.

n = 0

Usually not strong

Because 0! = 1, the factorial sum does not equal 0 in this variant.

Performance

Avoid repeated factorial work

Use the digit-factorial lookup table instead of recalculating each time.

Range size

Use wider type if needed

For very large ranges, use long for safer accumulation.

⏱️ Time and space complexity

TaskTimeExtra space
Single strong checkO(d) where d is number of digitsO(1)
Range 1..U scanO(U log U)O(1)

Summary

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

Classic strong numbers in base-10 are 1, 2, 145, and 40585.

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