Python Program to Check if a Number is a Strong Number

This is a Python Program to check if a number is a strong number.

Problem Description

The program takes a number and checks if it is a strong number.

Problem Solution

1. Take in an integer and store it in a variable.
2. Using two while loops, find the factorial of each of the digits in the number.
3. Then sum up all the factorials of the digits.
4. Check if the sum of the factorials of the digits is equal to the number.
5. Print the final result.
6. Exit.

Program/Source Code

Here is source code of the Python Program to check if a number is a strong number. The program output is also shown below.

 
sum1=0
num=int(input("Enter a number:"))
temp=num
while(num):
    i=1
    f=1
    r=num%10
    while(i<=r):
        f=f*i
        i=i+1
    sum1=sum1+f
    num=num//10
if(sum1==temp):
    print("The number is a strong number")
else:
    print("The number is not a strong number")
Program Explanation

1. User must enter the number and store it in a variable.
2. A copy of the original number is made as the original value will get altered in the later course of the program.
3. Using a while loop, each of the digits of the numbers is obtained.
4. Then the other while loop is used to find the factorial of the individual digits and store it in a sum variable.
5. If the sum of the factorial of the digits in a number is equal to the original number, the number is a strong number.
6. The final result is printed.

advertisement
advertisement
Runtime Test Cases
 
Case 1:
Enter a number:145
The number is a strong number.
 
Case 2:
Enter a number:234
The number is not a strong number.

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.