Python program to check given number is Disarium or not

In this tutorial, we will be learning about Disarium numbers and also see how to check if a number is a disarium number or not using a Python program. We will be using the following Python concepts to solve this problem:

  • Python if…else Statement
  • Loops inPpython
  • Functions

Disarium Number

A number is called a Disarium number is the sum of the digits powered to their respective positions is equal to the original number.
Examples: 89, 175, 518, etc.
Explanations: 175 = 11+ 72 + 53 = 1+ 49 + 125 = 175

To solve this problem we will take a user inputted number, find its length and then find the sum of the digits powered to their respective positions. Finally, we would compare this number with the original number.

# calculateLength() will find the length of a number    
def calculateLength(n):    
    len = 0  
    while(n != 0):    
        len = len + 1
        n = n // 10
    return len
     
num = int(input("Enter a number: "))    
digit = sum = 0 
len = calculateLength(num); 
     
# Makes a copy of the original number   
temp = num    
     
# Calculates the sum of digits powered to their respective position    
while(temp > 0):    
    digit = temp % 10    
    sum = sum + int(digit ** len)
    temp = temp // 10    
    len = len - 1   

# Checks whether the sum is equal to the number itself    
if sum == num :    
    print(num, "is a Disarium Number!!!")    
else:    
    print(num, "is not a Disarium Number!!!")

We have defined calculateLength() function to calculate the numbers of digits in the number passed to it as a parameter. Inside this function we initiate len as 0. This keeps count of the number of digits. To find the number of digits we start a while loop. We take the parameter n and increment len by 1, and then divide n by 10. We repeat this process till n becomes 0. Once n equals 0 we return len and come out of the loop. This link can be referred for any further doubts.

First, we ask the user to input the number using int(input(“Enter a number: “)). Here, input() prints the message on the console and also reads the input given as a string. But we want the input to be an int so that we can perform mathematical operations on them and for that we use int().

Now we initiate both sum and digit as 0, this also signifies that they are of int type. These variables are used later to calculate sum of the digits powered to their respective positions. We find the length of the number using calculateLength() by passing num as a parameter. We also make a copy of the inputted number in temp so that we can use it to compare with the sum later.

To calculate the sum we start a while loop in which we find each digit and then raise it to the power of its position using the len variable. We find each digit using mod(%) then we raise it to the power of its position using power operator(**) and then decrease len by 1 (for the next position). We also divide temp by 10 so that we can find the next digit. This continues till temp <= 0. This link can be referred for any further doubts.

After we come out of the loop we have the sum which we now compare with num. If they are equal then the inputted number is a Disarium Number. If not then the number is not. We print an appropriate message in either scenario.

Output

Enter a number: 175
175 is a Disarium Number!!!

Enter a number: 67
67 is not a Disarium Number!!!

So here it is, a simple program to check if a number is a disarium number or not with the help of Python programming.

Also read,

Leave a Reply

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