Strong Number: flowchart, algorithm and program

This program reads a number n and checks whether the sum of the factorials of its digits is equal to n. For example, 145 is a strong number because 1! + 4! + 5! = 1 + 24 + 120 = 145.

▶ Watch the dry run step by step →

Algorithm

  1. Step 1: Start.
  2. Step 2: Read n.
  3. Step 3: Set sum = 0, temp = n.
  4. Step 4: If temp > 0, go to step 5; otherwise go to step 12.
  5. Step 5: Set digit = temp % 10.
  6. Step 6: Set fact = 1, i = 1.
  7. Step 7: If i <= digit, go to step 8; otherwise go to step 10.
  8. Step 8: Set fact = fact * i.
  9. Step 9: Set i = i + 1. Go to step 7.
  10. Step 10: Set sum = sum + fact.
  11. Step 11: Set temp = temp / 10. Go to step 4.
  12. Step 12: If sum == n, go to step 13; otherwise go to step 14.
  13. Step 13: Print n is a Strong Number. Go to step 15.
  14. Step 14: Print n is not a Strong Number.
  15. Step 15: Stop.

Pseudocode

BEGIN
    READ n
    sum ← 0, temp ← n
    WHILE temp > 0 DO
        digit ← temp % 10
        fact ← 1, i ← 1
        WHILE i <= digit DO
            fact ← fact * i
            i ← i + 1
        END WHILE
        sum ← sum + fact
        temp ← temp / 10
    END WHILE
    IF sum == n THEN
        PRINT n is a Strong Number
    ELSE
        PRINT n is not a Strong Number
    END IF
END

Program in C, Python and Java

Strong Number program in C

#include <stdio.h>

int main() {
    int n, temp, digit, i;
    int sum = 0;
    printf("Enter n: ");
    scanf("%d", &n);
    temp = n;
    while (temp > 0) {
        digit = temp % 10;
        int fact = 1;
        for (i = 1; i <= digit; i++) {
            fact = fact * i;
        }
        sum = sum + fact;
        temp = temp / 10;
    }
    if (sum == n)
        printf("%d is a Strong Number\n", n);
    else
        printf("%d is not a Strong Number\n", n);
    return 0;
}

Strong Number program in Python

n = int(input("Enter n: "))
temp = n
total = 0
while temp > 0:
    digit = temp % 10
    fact = 1
    for i in range(1, digit + 1):
        fact = fact * i
    total = total + fact
    temp = temp // 10
if total == n:
    print(n, "is a Strong Number")
else:
    print(n, "is not a Strong Number")

Strong Number program in Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter n: ");
        int n = sc.nextInt();
        int temp = n;
        int sum = 0;
        while (temp > 0) {
            int digit = temp % 10;
            int fact = 1;
            for (int i = 1; i <= digit; i++) {
                fact = fact * i;
            }
            sum = sum + fact;
            temp = temp / 10;
        }
        if (sum == n)
            System.out.println(n + " is a Strong Number");
        else
            System.out.println(n + " is not a Strong Number");
    }
}

Sample input: n = 145
Output: 145 is a Strong Number

How it works

  • Copy n into temp so the original value stays safe for the final comparison. sum starts at 0.
  • Inside the while (temp > 0) loop, digit = temp % 10 picks out the last digit of temp.
  • A small for loop from i = 1 to digit multiplies fact = fact * i, giving the factorial of that digit. fact is reset to 1 for every new digit, and 0! correctly stays 1.
  • sum = sum + fact adds that factorial to the running total, then temp = temp / 10 drops the last digit.
  • When temp reaches 0, all digits are used up. If sum == n, it prints Strong Number, otherwise not a Strong Number. For 145: 1 + 24 + 120 = 145, so it is strong.

Dry run (trace table)

StepLineBoxnsumtempdigitfactiOutput
13Start
27Read n145Enter n:
38sum = 0, temp = n1450145
49temp > 0?1450145
510digit = temp % 1014501455
611fact = 1, i = 11450145511
712i <= digit?1450145511
813fact = fact * i1450145511
912i = i + 11450145512
1012i <= digit?1450145512
1113fact = fact * i1450145522
1212i = i + 11450145523
1312i <= digit?1450145523
1413fact = fact * i1450145563
1512i = i + 11450145564
1612i <= digit?1450145564
1713fact = fact * i14501455244
1812i = i + 114501455245
1912i <= digit?14501455245
2013fact = fact * i145014551205

Time and space complexity

Time: O(d) · Space: O(1). The outer loop runs once per digit d of n (about log n), and the inner factorial loop runs at most 9 times, which is constant work per digit.

Common mistakes

  • Forgetting to reset fact = 1 for each digit, so the factorial of the previous digit carries over and the sum goes wrong.
  • Comparing sum with temp instead of n. By then temp has become 0, so the check always fails.
  • Starting the factorial loop at 0, which makes fact become 0 for every digit. It must start at i = 1.

Viva questions

Q1. What is a strong number? Give an example.

A strong number is one whose digits' factorials add up to the number itself. For example 145 = 1! + 4! + 5! = 1 + 24 + 120 = 145.

Q2. Why do you copy n into temp on line 8?

The loop destroys the number by dividing it by 10 each time. I need the original n at the end to compare with sum, so I work on a copy.

Q3. What do temp % 10 and temp / 10 do here?

temp % 10 gives the last digit and temp / 10 is integer division that drops the last digit. Together they peel the digits off one by one until temp becomes 0.

Q4. Why is sum initialised to 0 and fact initialised to 1? Why is fact declared inside the loop?

sum is an accumulator for addition, so 0 is the neutral start. fact is a product, so it starts at 1, because 0 would make every product 0. It is declared inside the while loop so it is reset to 1 for every new digit.

Q5. What does the program print for a digit 0, for example n = 10? Why?

For digit 0 the for loop does not run (1 <= 0 is false), so fact stays 1, which correctly gives 0! = 1. For n = 10, sum = 1 + 1 = 2, which is not equal to 10, so it is not strong.

Q6. What happens if the user enters 0 or a negative number?

The while (temp > 0) condition is false straight away, so sum stays 0. For n = 0 the program wrongly says it is a Strong Number since 0 == 0. A negative number is reported as not strong. A check for n <= 0 would be needed to handle these.

Q7. What is the time complexity of this program?

The while loop runs d times, where d is the number of digits (about log10 n). The inner for loop runs at most 9 times, which is constant. So the complexity is O(log n).

Q8. What would you change to compute the factorials faster, or to avoid recomputing them for each digit?

Since digits are only 0 to 9, I could precompute a table fact[10] once and just look up fact[digit]. That removes the inner for loop, and each digit is handled in constant time.

Frequently asked questions

What is a strong number?

A strong number is a number whose digits' factorials add up to the number itself. 145 is one: 1! + 4! + 5! = 145. The other well-known ones are 1, 2 and 40585.

Why do we store n in temp?

The loop keeps dividing by 10 until the number becomes 0, which destroys the value. Working on temp keeps n intact for the final sum == n check.

What is the factorial of a digit 0 in this program?

0! is 1. Since fact starts at 1 and the for loop does not run when digit is 0, fact stays 1, which is the correct value.

More programs

← All programs

Try AI Flowchart free →