Python program to check if the given number is a Disarium Number


When it is required to check if a given nmber is a disarium number, the sum of digits powered to their respective position is computed. Before this, the number of digits present in the number is determined.

A Disarium Number is the one where the sum of its digits to the power of their respective position is equal to the original number itself.

Below is a demonstration for the same −

Example

 Live Demo

def length_calculation(num_val):
   length = 0
   while(num_val != 0):
      length = length + 1
      num_val = num_val//10
   return length
my_num = 192
remaining = sum_val = 0
len_val = length_calculation(my_num)
print("A copy of the original number is being made...")
num_val = my_num
while(my_num > 0):
   remaining = my_num%10
   sum_val = sum_val + int(remaining**len_val)
   my_num = my_num//10
   len_val = len_val - 1
if(sum_val == num_val):
   print(str(num_val) + " is a disarium number !")
else:
   print(str(num_val) + " isn't a disarium number")

Output

A copy of the original number is being made...
192 isn't a disarium number

Explanation

  • A method named ‘length_calculation’ is defined, that calculates the number of digits in the number.
  • It computes the floor division of the number and returns the length of the number.
  • The number is defined, and is displayed on the console.
  • It uses the modulus operation to get the remainder, and adds it to a sum variable.
  • The power of the position is multiplied with the number itself.
  • This is compared with the number.
  • If it is equal, it means it is a Harshad number, otherwise it is not.

Updated on: 12-Mar-2021

175 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements