Python Program to check a number is a disarium number

March 9, 2022, Learn eTutorial

In this simple python program, we need to check the given number is a Disarium number or not. It is a number python program.

To understand this example, you should have knowledge of the following Python programming topics:

What is a disarium number?

This simple python program is about to check for a Disarium number. Do we have to understand what a Disarium number is? Disarium number is a number that is similar to Armstrong's number. A number is called a disarium number if the sum of its digits powered with their position will be the number itself.

Let us take an example to make it clear.135 is a disarium number because the sum of cube of 5 + square of 3 + 1, that is equal to 125 + 9 + 1 = 135.

disarium number Checking


How to check for a disarium number in python?

In this simple python program for a disarium number, we need to check the given number is disarium or not. So we are using a user-defined function in python to find the length of the number. Then use a while loop in python till the number is greater than zero. To split the number, we use the mod operator by 10, and take the remainder in every split, then we find the sum as the sum of the remainder raise to length. Finally, decrement the length by 1 and divide the number by 10 to remove the last digit in each loop's iteration. Finally, we check the sum and the number is the same or not using an if condition in python programming.

disarium number Checking

Inside the 'user function' to find the length of the number; we use a while loop until the number is not zero. And we add the length by 1. Now we divide the number by 10 removing one digit from the number in every loop iteration. Finally, return the length to the main program.


STEP 1: Assign a number to a variable. We can use the input function also if we want to enter the number to check for disarium.

STEP 2: initialize sum and reminder as zero before starting the while loop in python language.

STEP 3: Calculate the length of the number using a user-defined function and passing the number as a parameter to the function.

STEP 4: We copy the number to a variable.

STEP 5: Open a while loop until the number is greater than zero.

STEP 6: Using the mod operator extracts the remainder of the number.

STEP 7: calculate the sum: the sum of reminder raise to length and previous sum.

STEP 8: Split the number by dividing it by 10 to get the next digit using python.

STEP 9: Decrement the length by one to get the next digit raised length.

STEP 10: Check the sum and num are equal using an 'if condition' if so, print its disarium number.

STEP 11: Else print not a disarium number.

USER DEFINED FUNCTION calculateLength(n)

STEP 1: First, we initialize a variable to zero for length.

STEP 2: Open a while loop until the number is not equal to zero.

STEP 3: Increment the length by one in every iteration.

STEP 4: Split the number by dividing it with 10 to remove the counted digit.

STEP 5: Return the length using python programming syntax.

Python Source Code

                                          def calculateLength(n):    
    length = 0;    
    while(n != 0):    
        length = length + 1;       # user defined function for calculating length
        n = n//10;    
    return length;    
num = 89;    
rem = sum = 0;    
len = calculateLength(num);    
n = num;      # copy the real number to a variable
while(num > 0):    
    rem = num % 10;    
    sum = sum + int(rem**len);   # calculating the sum to check for disarium or not
    num = num//10;    
    len = len - 1;    
if(sum == n):    
    print(str(n) + " is a disarium number");   # use str() function to convert integer to string
    print(str(n) + " is not a disarium number");    


89 is a Disarium number