Python Program to Check if a Number is a Strong Number

PythonServer Side ProgrammingProgramming

Strong number is a number whose sum of all digits’ factorial is equal to the number ‘n’. Factorial implies when we find the product of all the numbers below that number including that number and is denoted by ! (Exclamation sign), For example: 5! = 5x4x3x2x1 = 120. When it is required to check if a number is a strong number or not, the remainder/modulus operator and the ‘while’ loop can be used.

Below is the demonstration of the same −

Example

 Live Demo

my_sum=0
my_num = 296
print("The number is")
print(my_num)
temp = my_num
while(my_num):
   i=1
   fact=1
   remainder = my_num%10
   while(i<=remainder):
      fact=fact*i
      i=i+1
   my_sum = my_sum+fact
   my_num=my_num//10
if(my_sum == temp):
   print("The number is a strong number")
else:
   print("The number is not a strong number")

Output

The number is
296
The number is not a strong number

Explanation

  • A sum is initialized to 0.

  • The number is defined and is displayed on the console.

  • The number is defined to a temporary variable.

  • The while loop is used where the remainder is determined.

  • The while loop is used again to see whether the iterator is less than or equal to the remainder.

  • If it is less, the ‘fact’ variable is multiplied with the iterator.

  • It is then incremented by 1.

  • The sum value is added to the ‘fact’ variable.

  • If the ‘temp’ variable and the sum are equal, it is considered a string number.

raja
Published on 19-Apr-2021 10:49:17
Advertisements