Program to check Strong Number

Last Updated : 15 Jun, 2026

Strong Numbers are the numbers whose sum of factorial of digits is equal to the original number. Given a number, the task is to check if it is a Strong Number or not.

Examples: 

Input: 145
Output: true
Explanation: 1! + 4! + 5! = 145

Input: 5314
Output: false
Explanation: 5! + 3! + 1! + 4! is not equal to 5314.

Try It Yourself
redirect icon

[Naive Approach] Digit Extraction with Factorial - O(d × log d) Time and O(log n) Space

A number is Strong if the sum of factorials of its digits equals the number itself. Extract each digit, compute its factorial using recursion, sum them up, and compare with original number.

  • Store original number in a variable.
  • Extract digits one by one using modulo and division.
  • Compute factorial of each digit using recursive function.
  • Add all factorials to sum.
  • Compare sum with original number.
C++
#include <bits/stdc++.h>
using namespace std;

// Recursively calculate factorial of a digit
int factorial(int n)
{
    // Base case
    if (n <= 1)
        return 1;

    return n * factorial(n - 1);
}

bool isStrong(int n)
{
    // Keep a copy of the original number
    int original = n;

    int sum = 0;

    // Process every digit of the number
    while (n > 0)
    {
        int digit = n % 10;

        // Add factorial of current digit
        sum += factorial(digit);

        n /= 10;
    }

    // A number is Strong if the sum of
    // factorials of its digits equals itself
    return sum == original;
}

int main()
{
    int n = 145;

    cout << (isStrong(n) ? "true" : "false");

    return 0;
}
Java
import java.util.*;

class GFG {
    // Recursively calculate factorial of a digit
    static int factorial(int n) {
        // Base case
        if (n <= 1)
            return 1;

        return n * factorial(n - 1);
    }

    static boolean isStrong(int n) {
        // Keep a copy of the original number
        int original = n;

        int sum = 0;

        // Process every digit of the number
        while (n > 0) {
            int digit = n % 10;

            // Add factorial of current digit
            sum += factorial(digit);

            n /= 10;
        }

        // A number is Strong if the sum of
        // factorials of its digits equals itself
        return sum == original;
    }

    public static void main(String[] args) {
        int n = 145;

        System.out.print(isStrong(n) ? "true" : "false");
    }
}
Python
# Recursively calculate factorial of a digit
def factorial(n):
    # Base case
    if n <= 1:
        return 1

    return n * factorial(n - 1)


def isStrong(n):
    # Keep a copy of the original number
    original = n

    sum_val = 0

    # Process every digit of the number
    while n > 0:
        digit = n % 10

        # Add factorial of current digit
        sum_val += factorial(digit)

        n //= 10

    # A number is Strong if the sum of
    # factorials of its digits equals itself
    return sum_val == original


if __name__ == "__main__":
    n = 145
    print("true" if isStrong(n) else "false")
C#
using System;

class GFG {
    // Recursively calculate factorial of a digit
    static int factorial(int n) {
        // Base case
        if (n <= 1)
            return 1;

        return n * factorial(n - 1);
    }

    static bool isStrong(int n) {
        // Keep a copy of the original number
        int original = n;

        int sum = 0;

        // Process every digit of the number
        while (n > 0) {
            int digit = n % 10;

            // Add factorial of current digit
            sum += factorial(digit);

            n /= 10;
        }

        // A number is Strong if the sum of
        // factorials of its digits equals itself
        return sum == original;
    }

    static void Main(string[] args) {
        int n = 145;

        Console.Write(isStrong(n) ? "true" : "false");
    }
}
JavaScript
// Recursively calculate factorial of a digit
function factorial(n) {
    // Base case
    if (n <= 1)
        return 1;

    return n * factorial(n - 1);
}

function isStrong(n) {
    // Keep a copy of the original number
    let original = n;

    let sum = 0;

    // Process every digit of the number
    while (n > 0) {
        let digit = n % 10;

        // Add factorial of current digit
        sum += factorial(digit);

        n = Math.floor(n / 10);
    }

    // A number is Strong if the sum of
    // factorials of its digits equals itself
    return sum === original;
}

// Driver code
let n = 145;
console.log(isStrong(n) ? "true" : "false");

Output
true

[Expected Approach] Using Precomputed Factorials - O(d) Time and O(1) Space

A number is Strong if sum of factorials of its digits equals the number itself. Precompute factorials of digits 0-9 in an array for O(1) lookup, extract digits, sum their factorials, and compare with original number.

  • Precompute factorials of digits 0 to 9 in an array.
  • Store original number in a variable.
  • Extract digits using modulo and division.
  • Add precomputed factorial of each digit to sum.
  • Compare sum with original number.
C++
#include <bits/stdc++.h>
using namespace std;

bool isStrong(int n)
{
    // Factorials of digits 0 to 9
    // precomputed to avoid recalculation
    int fact[10] = {
        1, 1, 2, 6, 24,
        120, 720, 5040, 40320, 362880
    };

    // Preserve the original number
    int original = n;

    int sum = 0;

    // Extract digits one by one
    while (n > 0)
    {
        int digit = n % 10;

        // Add factorial of current digit
        sum += fact[digit];

        n /= 10;
    }

    // Check whether the generated sum
    // matches the original number
    return sum == original;
}

int main()
{
    int n = 145;

    cout << (isStrong(n) ? "true" : "false");

    return 0;
}
Java
import java.util.*;

class GFG {
    static boolean isStrong(int n) {
        // Factorials of digits 0 to 9
        // precomputed to avoid recalculation
        int[] fact = {
            1, 1, 2, 6, 24,
            120, 720, 5040, 40320, 362880
        };

        // Preserve the original number
        int original = n;

        int sum = 0;

        // Extract digits one by one
        while (n > 0) {
            int digit = n % 10;

            // Add factorial of current digit
            sum += fact[digit];

            n /= 10;
        }

        // Check whether the generated sum
        // matches the original number
        return sum == original;
    }

    public static void main(String[] args) {
        int n = 145;

        System.out.print(isStrong(n) ? "true" : "false");
    }
}
Python
def isStrong(n):
    # Factorials of digits 0 to 9
    # precomputed to avoid recalculation
    fact = [
        1, 1, 2, 6, 24,
        120, 720, 5040, 40320, 362880
    ]

    # Preserve the original number
    original = n

    sum_val = 0

    # Extract digits one by one
    while n > 0:
        digit = n % 10

        # Add factorial of current digit
        sum_val += fact[digit]

        n //= 10

    # Check whether the generated sum
    # matches the original number
    return sum_val == original


if __name__ == "__main__":
    n = 145
    print("true" if isStrong(n) else "false")
C#
using System;

class GFG {
    static bool isStrong(int n) {
        // Factorials of digits 0 to 9
        // precomputed to avoid recalculation
        int[] fact = {
            1, 1, 2, 6, 24,
            120, 720, 5040, 40320, 362880
        };

        // Preserve the original number
        int original = n;

        int sum = 0;

        // Extract digits one by one
        while (n > 0) {
            int digit = n % 10;

            // Add factorial of current digit
            sum += fact[digit];

            n /= 10;
        }

        // Check whether the generated sum
        // matches the original number
        return sum == original;
    }

    static void Main(string[] args) {
        int n = 145;

        Console.Write(isStrong(n) ? "true" : "false");
    }
}
JavaScript
function isStrong(n) {
    // Factorials of digits 0 to 9
    // precomputed to avoid recalculation
    const fact = [
        1, 1, 2, 6, 24,
        120, 720, 5040, 40320, 362880
    ];

    // Preserve the original number
    let original = n;

    let sum = 0;

    // Extract digits one by one
    while (n > 0) {
        let digit = n % 10;

        // Add factorial of current digit
        sum += fact[digit];

        n = Math.floor(n / 10);
    }

    // Check whether the generated sum
    // matches the original number
    return sum === original;
}

// Driver code
let n = 145;
console.log(isStrong(n) ? "true" : "false");

Output
true
Comment