Strong Number Program in Python

Check Whether or Not the Number is a Strong Number in Python

Given an integer input the objective is to check whether or not the given integer input is a Strong Number based on whether is satisfies the condition or not. Therefore, we’ll write a program to Check Whether or Not the Number is a Strong Number in Python Language.

Example
Input : 145
Output : It's a Strong Number
strong number or not in python

Check Whether or Not the Number is a Strong Number in Python Language

Given an integer input, the objective is to check whether or not the given integer input is a Strong number or not using loops and recursion. In order to do so we’ll check if the number satisfies the definition of a Strong number mentioned below

Therefore, to solve the above mentioned problem, we have come up with few methods in Python Language. The methods are listed below,

  • Method 1: Using Simple Iteration
  • Method 2: Using Recursive Function

Let’s implement the above mentioned methods and learn about them in detail in the upcoming sections.

Method 1: Using Simple Iteration

Working

In this method we’ll use the concept of loops to check whether the given number is a Strong number or not. To do so we’ll break the number after extracting the last digit of the number with each iteration and keep appending the sum variable with it’s factorial. In the end we check whether or not it matches the original number.

For an integer input as the number, we perform the following

  • Create a List of all the factorial values from 1 to 9.
  • Run a While loop until temp == 0.
    • Extract the Last Digit using Modulo Operator.
    • Shorten the length of the temp using divide operator.
    • Append the factorial of the digit in sum variable.
  • Check if the Sum matches the original string.

Let’s implement the above mentioned logic in Python Language.

Python Code

Run
#Using Iteration
n =145
#save the number in another variable
temp=n
sum=0
f=[0]*10
f[0]=1
f[1]=1
for i in range(2,10): #precomputing the factorial value from 0 to 9 and store in the array.
    f[i]=f[i-1]*i

#Implementation
while(temp):
    r=temp%10 #r will have the vale u of the unit digit
    temp=temp//10
    sum+=f[r] #adding all the factorial

if(sum==n):
    print("Yes", n ,"is strong number")

else:
    print("No" , n, "is not a strong number")

Output

Yes 145 is a strong Number

Method 2: Using Recursive Function

Working

In this method we’ll use recursion to recursively iterate through recursive calls and perform the fore mentioned processes. We set the recursive step call and base case. Keeping in mind all the necessary steps, we declare a recursive function. To know more about recursion, Check out our page, Recursion in Python.

For a given integer input number, we perform the following,

  • Define a Function check_StrongNumber() which takes a number as an argument.
  • Set the base case as num == 0.
  • Set the step recursive call as check_StrongNumber(num//10).
  • Check of the returned value matches the original number.

Let’s implement the above mentioned Logic in Python Language.

Python Code

Run
#Using Recursion
def Factorial(num):
    if num<=0: return 1 else: return num*Factorial(num-1) sum=0 def check_StrongNumber(num): global sum if (num>0):
        fact = 1
        rem = num % 10
        check_StrongNumber(num // 10)
        fact = Factorial(rem)
        sum+=fact
    return sum
num=145
if (check_StrongNumber(num) == num):
    print("Yes",num,"is a strong Number.")
else:
    print("No",num,"is not a strong Number.")

Output

Yes 145 is strong number

44 comments on “Strong Number Program in Python”


  • shivam8565shukla

    import math
    a = input(“Enter a number : “)
    f = int(a)
    b = list(map(int, a))
    c = sum(math.factorial(i) for i in b)
    if(f==c):
    print(“This is a Strong Number”)
    else:
    print(“This is not a Strong Number”)


  • dandelatha412

    n=145
    sum=0
    temp=n
    while n>0:
    fact=1
    rem=n%10
    for i in range(1,rem+1):
    fact*=i
    sum+=fact
    n=n//10
    if sum==temp:
    print(“Strong number”)
    else:
    print(“Not Strong number”)


  • ashmisasi84

    n=int(input(“Enter n:”))
    sum=0
    temp = n
    while n>=1:
    rem=n%10
    fact=1
    for i in range(1,rem+1):
    fact=fact*i
    sum = sum + fact
    n=n//10
    if(sum==temp):
    print(temp,”is a Strong number”)
    else:
    print(temp,”is not a strong number”)


  • abhin4116

    n=int(input(“enter the num:”))
    n=abs(n)
    num=str(n)
    data=[]
    def factorial(k):
    if k==0:
    return 1
    elif k>=1:
    return k*factorial(k-1)
    l=len(num)
    for i in range(l):
    data.append(factorial(int(num[i])))
    y=len(data)-1
    def sumoflist(y):
    if y==0:
    return data[0]
    elif y>=1:
    return data[y]+sumoflist(y-1)
    if n==sumoflist(y):
    print(“its a strong number”)
    elif n!=sumoflist:
    print(“its not strong number”)


  • satwikagamidi

    num=int(input())
    original=num
    temp=0
    while num!=0:
    n=num%10
    temp+=math.factorial(n)
    num=num//10
    if(original==temp):
    print(“strong number”)
    else:
    print(“not a strong number”)


  • ravisurya46955

    N=int(input(“enter a NUmber: “))
    num=N
    sum=0
    while N>0:
    digit=N%10
    fact=1
    for i in range(1,digit+1):
    fact=fact*i
    sum=sum+fact
    N=N//10
    if sum==num:
    print(“strong number”)
    else:
    print(“not strong number”)


  • Neha

    def fact(n):
    fact=1
    for i in range(1,n+1):
    fact=fact*i
    return fact
    num=145
    temp=num
    sum=0
    while(temp!=0):
    digit=int(temp%10)
    factorial=fact(digit)
    sum=sum+factorial
    temp=int(temp/10)
    if(num==sum):
    print(‘it is a strong number’)
    else:
    print(‘it is not a strong number’)


  • Mahibhai

    n=40585
    d=[n]
    list1=list(map(int,str(d[0])))
    sum1=0

    for i in list1:
    fact=1
    for j in range(1,i+1):
    fact*=j
    sum1+=fact
    if sum1==n:
    print(“strong”)
    else:
    print(“not”)


  • Pranudh

    n = int(input())
    x = str(n)
    sum = 0
    for char in x:
    y = int(char)
    product=1
    for z in range(1,y+1):
    product*=z
    sum+=product
    if(sum==n):
    print(“strong number”)
    else:
    print(“not a strong number”)


    • Venkata Ramana

      num=int (input (“Enter the number:”)
      temp=num
      sum=0
      while(num!=0):
      digit=num%10
      fact=1
      for i in range(1,digit+1):
      fact=fact*i
      sum+=fact
      num=num//10
      if sum==temp:
      print(temp,”it is a strong number”)
      else:
      print(temp,”it is not”)


  • Amar

    a=input()
    b=[]
    for i in a:
    fact=1
    for j in range(1,int(i)+1):
    fact=fact*i
    b.append(fact)
    if sum(b)==int(a):
    print(“strong number”)
    else:
    print(“not strong number”)


  • Mayank Gupta

    n=int(input(“Enter the no”))
    p=n
    sum=0
    while(n>0):
    r=n%10
    fact=1
    for i in range(1,r+1):
    fact=fact*i
    n=n//10
    sum=sum+fact
    print(sum)
    if(p==sum):
    print(“it is a strong no”)
    else:
    print(“Not a strong no.”)


  • Sangram

    N = “40585”
    strong = 0
    for i in N:
    m = int(i)
    #print(m)

    fact = 1
    for j in range(1, m+1):
    fact = fact * j
    print(fact)
    strong = strong + fact
    print(“final answer is”, strong)
    if int(N) == strong:
    print(“{} number is Strong Number”.format(N))
    else:
    print(“Oho Please try again {} number is not a strong number”.format(N))