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

25 comments on “Strong Number Program in Python”


  • 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))


  • sandeep

    def strong(num):
    n=num
    summ=0
    for i in str(num):
    sum=1
    for j in range(1,int(i)+1):
    sum=sum*j
    summ=summ+sum
    if summ==n:
    print(n,”is a strong number”)
    else:
    print(n,”is not a strong number”)
    num=int(input())
    strong(num)


  • Sheshadri

    n=input()
    res=0

    for i in n:
    i=int(i)
    fact=1
    for j in range(1,i+1):
    fact*=j
    res+=fact
    if res==int(n):
    print(“Strong number”)
    else:
    print(“Not an Strong Number”)


  • gorutarun

    def fun(i):
    if i==1 or i==0:
    return 1
    else:
    return i*fun(i-1)
    n=input()
    s=0
    for i in range(len(n)):
    s+=fun(int(n[i]))
    if int(n)==s:
    print(“strong number”)
    else:
    print(“not a strong number”)


  • Thanya

    def factorial(x):
    facto=1
    for i in range(1, x+1):
    facto*=i
    return facto

    n=int(input(“Enter a number:”))
    m=[int(d) for d in str(n)]
    summ=0
    for i in range(0, len(m)):
    fact=factorial(m[i])
    summ+=fact
    if summ==n:
    print(“strong number”)
    else:
    print(“not a strong number”)


  • Bibhudutta

    num=int(input())
    temp=num
    factorialSum=0
    order=len(str(num))
    for i in range(0,order):
    rem=num%10
    num=num//10
    fact=1
    for j in range(1,rem+1):
    fact=fact*j

    factorialSum=factorialSum+fact

    if(factorialSum==temp):
    print(temp,’is Strong number’)
    else:
    print(temp,’is not Strong number’)


  • shubhamkrsinghssm

    num=int(input(“Enter a number”))
    sum=0
    temp=num
    while temp>0:
    f=1
    i=1
    rem=temp%10
    while i<=rem:
    f=f*1
    i=i+1
    Sum=sum+f
    temp=temp//10
    Print("sum is {}".format(sum))


  • mohammadnawaz111

    sum=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
    sum=sum+f
    num=num//10
    if(sum==temp):
    print("It is a strong number")
    else:
    print("It is not a strong number")


  • Ashish

    import math
    arr=[]
    num=input(“Enter Number: “)
    s=0
    for i in range(len(num)):
    arr.append(num[i])
    s=s+math.factorial(int(arr[i]))
    if s==int(num):
    print(“Number {} is a strong Number”.format(int(num)))
    else:
    print(“Number {} is not a strong Number”.format(int(num)))


  • Ayushi

    value = int(input(“Enter a number: “))
    num = [int(d) for d in str(value)]
    fact = 1
    sum_ = 0

    def Fact(i):
    if i == 1:
    return i
    else:
    return (Fact(i-1) * i)

    for i in num:
    sum_ = sum_ + Fact(i)
    if (sum_ == value):
    print(“{} is a Strong Number”.format(value))
    else:
    print(“{} is not a Strong Number”.format(value))