Python Program To Check Strong Number or Not
Strong number is a special number whose sum of the factorial of digits
is equal to the original number.
Example:
Given Number : 145
It is a Strong Nuumber.
Since: 1! + 4! + 5! = 145
1! = 1*1 = 1
4! = 1*2*3*4 = 24
5! = 1*2*3*4*5 = 120
Example:
Given Number : 125
It is Not a Strong Nuumber.
Since:
1! + 2! + 5! = 123
1! = 1*1 = 1
2! = 1*2 = 2
5! = 1*2*3*4*5 = 120
you can use the following algorithm:
- Read the input number.
- Create a copy of the input number.
- Initialize a variable sum to 0.
- While the given number is not equal to 0,perform the following steps:
- Get the last digit of the num by using the modulo operator (%).
- Calculate the factorial of the last digit and add it to the sum.
- Divide the num by 10 to remove the last digit.
- Check if the sum is equal to the copy number.
- If they are equal, the number is a Strong Number.
Otherwise, it is not. - Print the result.
Source Code
# Python Program to Check Strong Number or Not
num = int(input("Enter a Number To Check : "))
Sum = 0
Ncopy= num
while num!=0:
Rd = num % 10
Fact=1
for i in range(1,Rd+1,1):
Fact=Fact*i
Sum=Sum+Fact
num=num//10
if Sum==Ncopy:
print("It is a Strong Or Krishnamurthy Number .")
else:
print("It is Not a Strong or Krishnamurthy Number .")
Output
Sample Input :
Enter a Number To Check : 145
Sample Output:
It is a Strong or Krishnamurthy Number
Sample Input :
Enter a Number To Check : 123
Sample Output:
It is Not a Strong Krishnamurthy Number