Python Program to print disarium numbers between 1 and 100


March 21, 2022, Learn eTutorial
1402

What is a Disarium number?

Disarium number is a number in which the sum of the digits raised to the power of the respective position will be equal to the number itself. In this basic python program example, we need to know the Disarium number and how to print disarium numbers within a range. 

disarium number Checking

How to print Disarium numbers in a range in python?

Suppose we take the number 89; we have to check if it's a disarium number or not. So we have to add 8 power of 1 and 9 square, which is 8 + 81 = 89. So, number 89 is a disarium number. In this simple python program, we need to print all the disarium numbers between 1 and 100. So we have to check each number for disarium or not in that range.

We are using two user-defined functions to solve this program, one is for finding the length of the number and the other is for check the number is disarium or not. Then in the main program, we call the disarium number check function, the length function is called from the sumdigit function.

DIsarium Check Function: In this user function, we call another function to get the length of the number. We initialize two variables for the remainder and sum, Open a while loop until the number is equal to zero, take the remainder by using the mod operator. We use arithmetic operations such as add the sum = sum + reminder raised to length. After that, we divide the number by 10 to remove the last digit, and we decrement the length by one.

Length Function: Here, we initialize a variable as length to zero and open a while loop until the number is not equal to zero. We increment the length variable by 1 in each iteration, and we remove one digit from the number by dividing the number by 10. Then after the while loop ends, return the length using the parameter passing method in python language.

disarium number Checking

ALGORITHM

STEP 1: Initialize a variable for the result as zero.

STEP 2: Display a message " Disarium numbers between 1 and 100 are"

STEP 3: Use a for loop to check every number from 1 to 100 using the range method in python.

STEP 4: Call the function for checking the number for disarium and return the value from the function to a variable for the result.

STEP 5: Inside that calling function, it calls another function to calculate the number's length.

STEP 6: Use an 'if condition' to check the result is the same as the number. If so, print the number.

USER DEFINED FUNCTION sumDigit()

STEP 1: Initialize the remainder and sum as zero.

STEP 2: Call the function to calculate length.

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

STEP 4: Use the mod operator to split the digit from the number.

STEP 5: Calculate sum as sum + (rem**len).

STEP 6: Divide the number with 10 to remove one digit from the number.

STEP 7: Decrement the length by one and return the sum.

USER DEFINED FUNCTION Length(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 by 10 to remove the counted digit.

STEP 5: Return the length.


To print the Disarium numbers we need to know about the below python topics, please refer these topics for a better understanding

Python Source Code

                                          def Length(n):    
    length = 0;    
    while(n != 0):                    # calculating the length of the number
        length = length + 1;    
        n = n//10;    
    return length;    
     
#sumDigit()  
def sumdigit(num):    
    rem = sum = 0;    
    len = Length(num);     # checking the number is disarium or not
        
    while(num > 0):    
        rem = num;   
        sum = sum + (rem**len);    
        num = num//10;    
        len = len - 1;    
    return sum;    
      
result = 0;    
     

print("Disarium numbers between 1 and 100 are");    
for i in range(1, 101):           # printing disarium numbers
    result = sumdigit(i);    
        
    if(result == i):    
        print(i),   
                                      

OUTPUT

Disarium numbers between 1 and 100 are

1  2  3  4  5  6  7  8  9  89