Python Interview Question

Strong Number in Python

Strong Number in Python
Strong Number in Python

Strong Number in Python

A Strong Number is a number whose value can be reproduced by adding the factorial of each of its individual digits. This is a useful programming problem for practicing digit extraction, loops, factorial calculations, and conditional statements in Python.

In this tutorial, we will understand the idea behind a Strong Number and create Python programs to check it using different approaches.

Strong Number in Python
Strong Number in Python

What Is a Strong Number?

Suppose a number contains several digits. Calculate the factorial of every digit separately and then add those factorials together. If the resulting total is exactly equal to the original number, that number is called a Strong Number.

Example: 145

Let’s break 145 into its individual digits:

  • Factorial of 1 = 1
  • Factorial of 4 = 24
  • Factorial of 5 = 120

Now add the results:

1 + 24 + 120 = 145

The calculated total is the same as the original number, so 145 is a Strong Number.

How to Check a Strong Number?

The basic logic is to process the number one digit at a time. A simple algorithm for this problem is:

  1. Read the number entered by the user.
  2. Keep a separate copy of the original number.
  3. Take the last digit from the working number.
  4. Calculate the factorial of that digit.
  5. Add the factorial to a running total.
  6. Remove the processed digit from the number.
  7. Repeat until all digits have been processed.
  8. Compare the factorial total with the original number.

Sample Results

For example, if the input is 132, the factorial sum does not match the original value, so it is not a Strong Number.

Enter a number: 132
Given number is not a Strong Number.

For the input 145, the factorial sum becomes 145.

Enter a number: 145
Given number is a Strong Number.

Python Program to Check a Strong Number

Example 1: Using Nested While Loops

The following program extracts every digit with the modulo operator and calculates its factorial using another loop.

num = int(input("Enter a number: "))

original = num
total = 0

while num > 0:
    digit = num % 10

    factorial = 1
    i = 1

    while i <= digit:
        factorial *= i
        i += 1

    total += factorial
    num //= 10

if total == original:
    print("Given number is a Strong Number.")
else:
    print("Given number is not a Strong Number.")

Output

Enter a number: 145
Given number is a Strong Number.

How This Program Works

  • original preserves the value that was entered by the user.
  • num % 10 obtains the rightmost digit.
  • The inner while loop calculates that digit’s factorial.
  • The calculated factorial is added to total.
  • num //= 10 removes the last digit from the working number.
  • Finally, total is compared with original.

Example 2: Using a For Loop for Factorial

The factorial calculation can also be written with a for loop. The outer loop still processes the digits one by one, while the inner loop handles multiplication from 1 up to the current digit.

num = int(input("Enter the number: "))

original = num
total = 0

while num > 0:
    digit = num % 10
    factorial = 1

    for i in range(1, digit + 1):
        factorial *= i

    print(f"Factorial of {digit} = {factorial}")

    total += factorial
    num //= 10

print(f"Sum of factorials of {original} = {total}")

if total == original:
    print("The given number is a Strong Number.")
else:
    print("The given number is not a Strong Number.")

Output

Enter the number: 145
Factorial of 5 = 120
Factorial of 4 = 24
Factorial of 1 = 1
Sum of factorials of 145 = 145
The given number is a Strong Number.

This version also displays the factorial calculated for each digit, making it easier to understand how the final result is produced.

Example 3: Using math.factorial()

Python’s math module already provides a factorial function. Using it avoids writing a separate loop for the factorial calculation.

import math

num = int(input("Enter the number: "))

original = num
total = 0

while num > 0:
    digit = num % 10

    factorial = math.factorial(digit)

    print(f"Factorial of {digit} = {factorial}")

    total += factorial
    num //= 10

print(f"Sum of factorials of {original} = {total}")

if total == original:
    print("The given number is a Strong Number.")
else:
    print("The given number is not a Strong Number.")

Output

Enter the number: 145
Factorial of 5 = 120
Factorial of 4 = 24
Factorial of 1 = 1
Sum of factorials of 145 = 145
The given number is a Strong Number.

Why Use math.factorial()?

The math.factorial() function performs the factorial calculation for us. This makes the program shorter and keeps the main Strong Number logic focused on extracting digits and adding their factorials.

Important Python Operators Used

Two operators play an important role in these programs:

  • Modulo (%) – obtains the last digit of a number. For example, 145 % 10 gives 5.
  • Floor Division (//) – removes the last digit. For example, 145 // 10 gives 14.

By repeatedly using these two operations, Python can process every digit without converting the number into a string.

Complete Advance AI Topics: Click Here
SQL Tutorial:
Click Here
YT:- DecodeIT

Strong Number Program: Method Comparison

ApproachFactorial CalculationMain Benefit
Nested While LoopInner while loopShows the complete logic step by step
For Loopfor loopProvides a compact factorial calculation
math.factorial()Built-in functionReduces the amount of code

Conclusion

A Strong Number is identified by comparing the original number with the sum of the factorials of all its digits. Python makes this problem a useful exercise for understanding loops, arithmetic operators, variables, and conditional statements.

We implemented the same concept in three ways: by calculating factorials with nested while loops, by using a for loop, and by calling math.factorial(). Once the digit-extraction logic is understood, the same approach can be adapted to many other number-based programming problems.

Frequently Asked Questions

What is a Strong Number?

A Strong Number is a number for which the sum of the factorials of its digits is equal to the number itself. For example, 145 is a Strong Number.

Is 145 a Strong Number?

Yes. The calculation is 1! + 4! + 5! = 1 + 24 + 120 = 145, so 145 satisfies the Strong Number condition.

How can I check a Strong Number using a while loop?

Extract each digit using % 10, calculate its factorial, add it to a total, and remove the digit using // 10. Finally, compare the total with the original number.

Can I use math.factorial() in a Strong Number program?

Yes. Import Python’s math module and use math.factorial(digit) to calculate the factorial of each digit.

Which loop can be used to find a Strong Number?

Both while and for loops can be used. The choice depends on how you want to structure the factorial and digit-processing logic.

Keywords

Strong Number in Python, Strong Number Python program, Strong Number using for loop,Strong Number in Python, Strong Number using while loop, check Strong Number in Python, Strong Number in Python,Strong Number example, program to check Strong Number in Python, Python factorial program, Strong Number using function, what is Strong Number, Strong Number logic in Python, Python number programs, factorial of digits Python, Strong Number algorithm, Python programming examples,Strong Number in Python,Strong Number in Python

Source Code Available

Interested in This Project?

Get the complete source code for this project at a very affordable price — perfect for your portfolio, college submission, or learning. Message us on WhatsApp and we'll get back to you instantly!

Full source code included Step-by-step setup guide Instant delivery on WhatsApp Instant reply on WhatsApp
Chat on WhatsApp

We usually reply within a few minutes

Leave a Reply

Your email address will not be published. Required fields are marked *

Chat with us