Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

Python Program to Check Strong Number

Posted in Python Tutorial
Updated on Jan 11, 2024
By Mari Selvan
πŸ‘οΈ 115 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
Python Program to Check Strong Number

Photo Credit to CodeToFun

πŸ™‹ Introduction

In the world of programming, exploring number properties is a fascinating journey. One such property is related to Strong Numbers.

A Strong Number (also known as a Digital Factorial) is a number in which the sum of the factorials of its digits is equal to the number itself.

In this tutorial, we'll dive into a Python program that checks whether a given number is a Strong Number or not.

πŸ“„ Example

Let's explore the Python code that checks whether a given number is a Strong Number.

is_strong_number.py
Copied
Copy To Clipboard
# Function to calculate the factorial of a number
def factorial(num):
    if num == 0 or num == 1:
        return 1
    else:
        return num * factorial(num - 1)

# Function to check if a number is a Strong Number
def is_strong_number(num):
    original_num = num
    total_sum = 0

    while num > 0:
        digit = num % 10
        total_sum += factorial(digit)
        num //= 10

    return total_sum == original_num

# Driver program
if __name__ == "__main__":
    # Replace this value with your desired number
    number = 145

    # Check if the number is a Strong Number
    if is_strong_number(number):
        print(f"{number} is a Strong Number.")
    else:
        print(f"{number} is not a Strong Number.")

πŸ’» Testing the Program

To test the program with different numbers, simply replace the value of number in the if __name__ == "__main__": block.

Output
145 is a Strong Number.

Run the script to check if the number is a Strong Number.

🧠 How the Program Works

  1. The program defines a function factorial to calculate the factorial of a number.
  2. Another function is_strong_number is defined to check if a given number is a Strong Number.
  3. Inside the is_strong_number function, it iterates through each digit of the number, calculates the factorial, and adds it to the sum.
  4. The result is compared with the original number to determine if it is a Strong Number.

πŸ“ Between the Given Range

Let's take a look at the python code that checks for strong numbers in the range of 1 to 200.

is_strong_number.py
Copied
Copy To Clipboard
# Function to calculate the factorial of a number
def factorial(num):
    if num == 0 or num == 1:
        return 1
    else:
        return num * factorial(num - 1)

# Function to check if a number is a strong number
def is_strong_number(n):
    original_num = n
    digit_sum = 0

    while n > 0:
        digit = n % 10
        digit_sum += factorial(digit)
        n //= 10

    return digit_sum == original_num

# Driver program
# Check for strong numbers in the range 1 to 200
strong_numbers = [num for num in range(1, 201) if is_strong_number(num)]

# Output the strong numbers
print("Strong Numbers in the Range 1 to 200:")
print(" ".join(map(str, strong_numbers)))

πŸ’» Testing the Program

Output
Strong Numbers in the Range 1 to 200:
1 2 145

Run the program to see the strong numbers in the range of 1 to 200.

🧠 How the Program Works

  1. The program defines a function factorial to calculate the factorial of a number.
  2. It then defines a function is_strong_number to check if a given number is a strong number.
  3. The main section of the code checks for strong numbers in the range from 1 to 200 using a list comprehension.
  4. The list of strong numbers is then printed in the specified output format.

🧐 Understanding the Concept of Strong Number

To determine if a number is a Strong Number, we need to calculate the factorial of each digit of the number and then sum these factorials. If the sum is equal to the original number, then it is a Strong Number.

For example, let's consider the number 145:

  • The factorial of 1 is 1.
  • The factorial of 4 is 24.
  • The factorial of 5 is 120.

The sum of these factorials is 1 + 24 + 120 = 145, which is the original number. Hence, 145 is a Strong Number.

πŸŽ‰ Conclusion

Understanding and implementing programs that check for mathematical properties, such as Strong Numbers, enhances problem-solving skills in programming. Feel free to modify and expand upon this code for your specific needs. Happy coding!

πŸ‘¨β€πŸ’» Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
10 months ago

If you have any doubts regarding this article (Python Program to Check Strong Number), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy