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

Created with Sketch.

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

A number is said to be the Disarium number when the sum of its digit raised to the power of their respective positions becomes equal to the number itself.

For example, 175 is a Disarium number as follows:

11+ 72 + 53 = 1+ 49 + 125 = 175

ALGORITHM:

  • STEP 1: calculateLength() counts the digits present in a number.
    1. Use a while loop to check whether the number is not equal to 0.
    2. Divide the number by 10 and increment the variable length by 1.
    3. Return length.
  • STEP 2: Define and initialize variable num.
  • STEP 3: Make a copy of num by storing the value of num in n.
  • STEP 4: Using while loop calculates remainder rem repeatedly by dividing num with 10.
  • STEP 5: Calculate the value of rem raised to power its position, i.e., remlen and store the computed value in a variable sum.
  • STEP 6: Check whether the sum is equal to the number. If yes, then given number is Disarium number. Else, it is not a Disarium number.

PROGRAM:

  1. #calculateLength() will count the digits present in a number  
  2. def calculateLength(n):
  3.     length = 0;
  4.     while(n != 0):
  5.         length = length + 1;
  6.         n = n//10;
  7.     return length;
  8. num = 175;
  9. rem = sum = 0;
  10. len = calculateLength(num);
  11. #Makes a copy of the original number num  
  12. n = num;
  13. #Calculates the sum of digits powered with their respective position  
  14. while(num > 0):
  15.     rem = num%10;
  16.     sum = sum + int(rem**len);
  17.     num = num//10;
  18.     len = len – 1;
  19. #Checks whether the sum is equal to the number itself  
  20. if(sum == n):
  21.     print(str(n) + ” is a disarium number”);
  22. else:
  23.     print(str(n) + ” is not a disarium number”);

Output:

175 is a disarium number

Leave a Reply

Your email address will not be published. Required fields are marked *