# Print all Disarium numbers within given range in Python

In this tutorial, we will learn to **print all Disarium numbers within the given range using Python**. Here we will learn what is a Disarium number, how to find whether a number is a Disarium number or not, and also a Python program to implement the same. If you want to get the Python program to print all the Disarium numbers within a given range, you are at the right place.

## What is a Disarium number?

If the sum of the individual digits of a number raised to the power of its position equals the number, then it is a Disarium number. To understand what a Disarium number is, we will take an example-

- Suppose, the number is 135. Here, the positions of digits 1, 3, and 5 are 1, 2, and 3 respectively.

So, 1^1 + 3^2 + 5^3 = 1 + 9 + 125 = 135

So, the result is equal to the number we have taken. Therefore, the number**135 is a Disarium number**.

Let us take another example for a better understanding of the above concept.

- Suppose, the number is 130. Here, the positions of digits 1, 3, and 0 are 1, 2, and 3 respectively.

So, 1^1 + 3^2 + 0^3 = 1 + 9 + 0 = 10

So, the result is not equal to the number we have taken. Therefore, the number**130 is not a Disarium number**.

## Method to find if a number is a Disarium number

So, we will learn to check whether a number is a Disarium number or not. We can check if a number is a Disarium number by following the steps given below-

**Steps to calculate the number of digits in the number-**- Divide the number by 10.
- Increment the digit count by 1.
**Steps to check whether the number is a Disarium number-**- Declare a variable to store the sum.
- Calculate the remainder after dividing the number by 10.
- Multiply the remainder by the number of digits.
- Add the result to the sum.
- Divide the number by 10.
- Decrement the number of digits by 1.
- Check if the sum is equal to the number.
- If the comparison results true, the number is a Disarium number.
- Otherwise, the number is not a Disarium number.

## Python program to print all Disarium numbers within the given range

Now, we will see a Python program to display Disarium numbers within the range. Here, we will take the lower and upper bound of the range from the user. Using a *‘for’* loop, we traverse from lower bound to upper bound and check whether the number is a Disarium number or not. The Python program is as follows-

def check_disarium(number): no_digits = 0 num = number while(num != 0): no_digits = no_digits + 1 num = num//10 remainder = 0 disarium_num = 0 while(number > 0): remainder = number % 10 disarium_num = disarium_num + (remainder**no_digits) number = number//10 no_digits = no_digits - 1 return disarium_num lower = int(input("ENTER LOWEST NUMBER : ")) upper = int(input("ENTER HIGHEST NUMBER : ")) print("DISARIUM NUMBERS WITHIN RANGE({},{}) ARE -".format(lower,upper)) for i in range(lower,upper+1): if check_disarium(i) == i: print(i,end=" ")

#### Python program output

The above Python program prints Disarium numbers within the range given by the user. The output of the program is-

siddharth@siddharth-Lenovo-Y520-15IKBN:~/python$ python3 disarium.py ENTER LOWEST NUMBER : 1 ENTER HIGHEST NUMBER : 200 DISARIUM NUMBERS WITHIN RANGE(1,200) ARE - 1 2 3 4 5 6 7 8 9 89 135 175 siddharth@siddharth-Lenovo-Y520-15IKBN:~/python$

In this sample execution, the lower and upper bound entered by the user is 1 and 200 respectively. So, in the range (1, 200), there are 12 Disarium numbers- 1, 2, 3, 4, 5, 6, 7, 8, 9, 89, 135, 175.

Also read: Print all the peaks and troughs in a list of Integers in Python

## Leave a Reply

You must be logged in to post a comment.