Python Basic
Python Interview Programs
- Python Interview Programs
- Python Abundant Number
- Python Amicable Number
- Python Armstrong Number
- Python Average of N Numbers
- Python Automorphic Number
- Python Biggest of three numbers
- Python Binary to Decimal
- Python Common Divisors
- Python Composite Number
- Python Condense a Number
- Python Cube Number
- Python Decimal to Binary
- Python Decimal to Octal
- Python Disarium Number
- Python Even Number
- Python Evil Number
- Python Factorial of a Number
- Python Fibonacci Series
- Python GCD
- Python Happy Number
- Python Harshad Number
- Python LCM
- Python Leap Year
- Python Magic Number
- Python Matrix Addition
- Python Matrix Division
- Python Matrix Multiplication
- Python Matrix Subtraction
- Python Matrix Transpose
- Python Maximum Value of an Array
- Python Minimum Value of an Array
- Python Multiplication Table
- Python Natural Number
- Python Number Combination
- Python Odd Number
- Python Palindrome Number
- Python Pascalβs Triangle
- Python Perfect Number
- Python Perfect Square
- Python Power of 2
- Python Power of 3
- Python Pronic Number
- Python Prime Factor
- Python Prime Number
- Python Smith Number
- Python Strong Number
- Python Sum of Array
- Python Sum of Digits
- Python Swap Two Numbers
- Python Triangular Number
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.
# 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.
145 is a Strong Number.
Run the script to check if the number is a Strong Number.
π§ How the Program Works
- The program defines a function factorial to calculate the factorial of a number.
- Another function is_strong_number is defined to check if a given number is a Strong Number.
- Inside the is_strong_number function, it iterates through each digit of the number, calculates the factorial, and adds it to the sum.
- 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.
# 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
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
- The program defines a function factorial to calculate the factorial of a number.
- It then defines a function is_strong_number to check if a given number is a strong number.
- The main section of the code checks for strong numbers in the range from 1 to 200 using a list comprehension.
- 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:
Author
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
If you have any doubts regarding this article (Python Program to Check Strong Number), please comment here. I will help you immediately.