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

28 comments on “Strong Number Program in Python”


  • munigala

    import math

    n=int(input())
    a=str(n)
    b=0
    for i in range(len(a)):
    b+=math.factorial(int(a[i]))

    if n==b:
    print(“yes, is strong number”)
    else:
    print(“no”)


  • Aditya

    def strong_number(n):
    num=str(n)
    sum=0
    for i in num:
    facto=1
    check=int(i)
    for j in range(1,check+1):
    facto*=j
    sum+=facto
    if sum==n:
    print(f”{n} is a strong number”)
    else:
    print(f”{n} is not a strong number”)


  • Ahamed

    def fact(n):
    m = int(n)
    k = 1
    for i in range(1, (m+1)):
    k = k*i
    return k
    n1 = input()
    g = 0
    for i in n1:
    g = g+fact(i)
    if g == int(n1):
    print(‘Strong number {}’.format(n1))
    else:
    print(‘Not Strong number {}’.format(n1))


  • asatiayush0

    # Strong Number
    def factorial(n):
    return 1 if n == 1 or n == 0 else n * factorial(n-1)
    number = input(“Enter number: “)
    temp = [int(d) for d in number]
    sum = 0
    for i in temp:
    sum += factorial(i)
    if sum == int(number):
    print(“Strong Number”)
    else:
    print(“Not Strong”)


  • L_50_Vegi.Deekshita

    a=input().strip()
    f=1
    s=0
    b=[]
    c=[]
    for i in a:
    b.append(int(i))
    for i in b:
    for j in range(1,i+1):
    f=f*j
    c.append(f)
    f=1
    for i in c:
    s+=i
    if(s==int(a)):
    print(“Strong number”)
    else:
    print(“Not a strong number”)


  • L_50_Vegi.Deekshita

    a=input().strip()
    f=1
    s=0
    b=[]
    c=[]
    for i in a:
    b.append(int(i))
    for i in b:
    for j in range(1,i+1):
    f=f*j
    c.append(f)
    f=1
    for i in c:
    s+=i
    print(s)


  • sandesh

    def fac(i):
    if i==0:
    return 1
    elif i>0:
    return i*fac(i-1)
    n=int(input(‘enter the no ‘))
    n1=str(n)
    sum1=0
    for j in n1:
    sum1=sum1+fac(int(j))

    if sum1==n:
    print(‘power no’)
    else:
    print(‘not’)


  • Lalit

    # program to find the strong number:
    # strong numbers are those numbers whose individual digit’s factorial is equal to original numbers

    # factorial function:
    def fact(num):
    temp=1
    for i in range(1,num+1):
    temp=temp*i
    return (temp)

    #function to [split the digits]

    def split_to_check(num):
    temp_original=num
    result=0
    while num!=0:
    remainder=num%10
    result=result+ fact(remainder)
    num=num//10
    if temp_original==result:
    print(“Number is STRONG”,temp_original,result)
    else:
    print(“Number is NOT strong”,temp_original,result)

    #input
    num=int(input(“Enter a number to check: “))

    split_to_check(num)


  • Shubhanjay

    num=int(input(“Enter a Num:- “))
    array=[int(d)for d in str(num)]
    sum=0

    def factorial(val):
    fact = 1
    for i in range(1,val+1):
    fact= fact*i
    return fact

    for digit in array:
    factorial(digit)
    sum=sum+factorial(digit)
    if sum==num:
    print(num,” is a Strong Number”)
    else:
    print(num, “is not a Strong Number”)


  • PRERNA

    a=int(input(“enter a number”))
    sum=0
    temp=a
    while(a):
    i=1
    factorial=1
    remainder=a%10
    while(i<=remainder):
    factorial=factorial*i
    i+=1
    sum=sum+factorial
    a=a//10
    if(temp==sum):
    print("the number provided is a strong number")
    else:
    print("the number provided is not a strong number")


  • gaurav

    n = input(“n: “)
    sum = 0
    for i in n:
    k = 1
    for j in range(1,int(i)+1):
    k = k*j
    sum+= k

    if sum == int(n):
    print(“strong number”)


  • Pavithra

    import math
    d=0
    k=input()
    u=[l for l in k]
    for i in u:
    v=int(i)
    d=d+(math.factorial(v))
    if(int(k)==int(d)):
    print(“strong”)
    else:
    print(“weak”)