Strong number checker in python

Strong number program in python | python basic programs | learn python | Codigence
codigence- strong number checker in python


What is a Strong number?


A Strong number is a special number whose sum of the all digit factorial should be equal to the number itself.

To find a whether given number is strong or not. We pick each digit from the given number and find its factorial, and we will do this every digit of the number.

Once we get the factorial of all digit, then we do the sum of factorials. If the sum is equal to the given number then the given number is strong otherwise not.

Code 1(Using While Loop): 

 
# Variable to store sum of the numbers  
sum=0  
# Ask user to enter the number  
num=int(input("Enter a number:"))  
# temporary variable  store copy of the original number  
dup_no=num  
# Using while loop  
while(dup_no):  
    # intialize with 1  
    i=1  
    # factorial variable with 1  
    fact=1  
    rem=dup_no%10  
    while(i<=rem):  
        fact=fact*i   # Find factorial of each number  
        i=i+1  
    sum=sum+fact  
    dup_no=dup_no//10  
if(sum==num):  
    print(str(num)+" is a strong number")  
else:  
    print(str(num)+" is not a strong number")
 
 
 

Output: 

  
Enter a number:
145
145 is a strong number
  
  

Code 2(Using For Loop):

 
# Python Program to find Strong Number  
num = int(input(" Enter the Number:"))  
sum = 0  
dup_no = num  
  
while(dup_no > 0):  
    fact = 1  
    rem = dup_no % 10  
  
    for i in range(1, rem + 1):  
        fact = fact * i  
  
    print("Factorial of %d = %d" %(rem, fact))  
    sum = sum + fact  
    dup_no = dup_no // 10  
  
print("\nSum of Factorials of a Given Number %d = %d" %(num, sum))  
      
if (sum == num):  
    print("So %d is a Strong Number"%(num))  
else:  
    print("So %d is not a Strong Number"%(num))

Output: 


 
Enter the Number:
145
Factorial of 5 = 120
Factorial of 4 = 24
Factorial of 1 = 1

Sum of Factorials of a Given Number 145 = 145
So 145 is a Strong Number

Code 3(Using math library): 

 
# using math library
import math  
num = int(input(" Enter the Number:"))  
sum = 0  
dup_no = num  
  
while(dup_no > 0):  
    rem = dup_no % 10  
    fact = math.factorial(rem)  # Using the buitlt-in factorial function  
  
    print("Factorial of %d = %d" %(rem, fact))  
    sum = sum + fact  
    dup_no = dup_no // 10  
  
print("\n Sum of Factorials of a Given Number %d = %d" %(num, sum))  
      
if (sum == num):  
    print(" %d is a Strong Number"%(num))  
else:  
    print(" %d is not a Strong Number"%(num))

Output: 

 
Enter the Number:
145
Factorial of 5 = 120
Factorial of 4 = 24
Factorial of 1 = 1

 Sum of Factorials of a Given Number 145 = 145
 145 is a Strong Number