;

Python Program to Check if the Given Number is a Strong Number or Not


Tutorialsrack 25/04/2020 Python

In this Python program, we will learn how to check if the given number is a strong number or not.

What is a Strong Number?

A Strong Number is a number in which the sum of the factorial of individual digits of that number is equal to the original number. For examples : 1, 2, 145, 40585, etc.

Now take a look at that example, we have a number 145 and its digits are 1, 4, and 5.

Now we find the factorial of each digit like we do

1! = 1 = 1

4! = 4*3*2*1 = 24

5! = 5*4*3*2*1 = 120

Now their sum = 120 + 24 + 1 = 145 which is equal to the Original Number.

Hence We can Say that 145 is a Strong Number.

Here is the source code of the program to check if the given number is a strong number or not.

Program 1: Python Program to Check if the Given Number is a Strong Number or Not - Using While Loop

Python Program to Check if the Given Number is a Strong Number or Not - Using While Loop
# Python Program to Check if the Given Number is a Strong Number or Not - Using While Loop

# Take The Input From the user
Number = int(input("Enter any Number: "))
Sum = 0
Temp = Number

while(Temp > 0):
    Factorial = 1
    i = 1
    Reminder = Temp % 10

    while(i <= Reminder):
        Factorial = Factorial * i
        i = i + 1

    print("\nFactorial of %d = %d" %(Reminder, Factorial))
    Sum = Sum + Factorial
    Temp = Temp // 10

print("\nSum of Factorials of a Given Number %d = %d" %(Number, Sum))
    
if (Sum == Number):
    print("%d is a Strong Number" %Number)
else:
    print("%d is not a Strong Number" %Number)
Output

Enter any 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

 

Enter any Number: 251

Factorial of 1 = 1

Factorial of 5 = 120

Factorial of 2 = 2

 

Sum of Factorials of a Given Number 251 = 123

251 is not a Strong Number

Program 2: Python Program to Check if the Given Number is a Strong Number or Not - Using For Loop

Python Program to Check if the Given Number is a Strong Number or Not - Using For Loop
# Python Program to Check if the Given Number is a Strong Number or Not - Using For Loop

# Take the Input from the User
Number = int(input("Enter any Number: "))
Sum = 0
Temp = Number

while(Temp > 0):
    Factorial = 1
    Reminder = Temp % 10

    for i in range(1, Reminder + 1):
        Factorial = Factorial * i

    print("Factorial of %d = %d" %(Reminder, Factorial))
    Sum = Sum + Factorial
    Temp = Temp // 10

print("\nSum of Factorials of a Given Number %d = %d" %(Number, Sum))
    
if (Sum == Number):
    print("%d is a Strong Number" %Number)
else:
    print("%d is not a Strong Number" %Number)
Output

Enter any 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



Enter any Number: 256

Factorial of 6 = 720

Factorial of 5 = 120

Factorial of 2 = 2

 

Sum of Factorials of a Given Number 256 = 842

256 is not a Strong Number


Related Posts



Comments

Recent Posts
Tags