Python Program to Print all Disarium Numbers within Given range

In the previous article, we have discussed Python Program to Get Tangent value Using math.tan()
Disarium Number:

A Disarium number is one in which the sum of each digit raised to the power of its respective position equals the original number.

like 135 , 89, etc.

Here 1^1 + 3^2 + 5^3 = 135 so it is disarium Number

Examples:

Example1:

Input:

Given lower limit range = 7
Given upper limit range = 180

Output:

The disarium numbers in the given range 7 and 180 are: 
7 8 9 89 135 175

Example 2:

Input:

Given lower limit range = 1
Given upper limit range = 250

Output:

The disarium numbers in the given range 1 and 250 are:
1 2 3 4 5 6 7 8 9 89 135 175

Program to Print all Disarium Numbers within Given range

Below are the ways to Print all Disarium Numbers within the given range.

Method #1: Using For Loop (Static input)

Approach:

  • Give the lower limit range as static input and store it in a variable.
  • Give the upper limit range as static input and store it in another variable.
  • Loop from lower limit range to upper limit range using For loop.
  • Inside the for loop take a variable say ‘num’ and initialize its value to the iterator value.
  • Make a copy of the number so you can verify the outcome later.
  • Make a result variable (with a value of 0) and an iterator ( set to the size of the number)
  • Create a while loop to go digit by digit through the number.
  • On each iteration, multiply the result by a digit raised to the power of the iterator value.
  • On each traversal, increment the iterator.
  • Compare the result value to a copy of the original number.
  • Print if the given number is a disarium number.
  • The Exit of the program.

Below is the implementation:

# Give the lower limit range as static input and store it in a variable.
lowlim_range = 7
# Give the upper limit range as static input and store it in another variable.
upplim_range = 180
print('The disarium numbers in the given range',
      lowlim_range, 'and', upplim_range, 'are:')
# Loop from lower limit range to upper limit range using For loop.
for m in range(lowlim_range, upplim_range+1):

    # given number
    num = m
    # intialize result to zero(ans)
    ans = 0

    # calculating the digits
    digit_s = len(str(num))
    # copy the number in another variable(duplicate)
    dup_numbr = num
    while (dup_numbr != 0):
        # getting the last digit
        remaindr = dup_numbr % 10
        # multiply the result by a digit raised to the power of the iterator value.
        ans = ans + remaindr**digit_s
        digit_s = digit_s - 1
        dup_numbr = dup_numbr//10
    # It is disarium number if it is equal to original number
    if(num == ans):
        print(num, end=' ')

Output:

The disarium numbers in the given range 7 and 180 are:
7 8 9 89 135 175

Method #2: Using For Loop (User input)

Approach:

  • Give the lower limit range as user input using the int(input()) function and store it in a variable.
  • Give the upper limit range as user input using the int(input()) function and store it in another variable.
  • Loop from lower limit range to upper limit range using For loop.
  • Inside the for loop take a variable say ‘num’ and initialize it’s value to  iterator value .
  • Make a copy of the number so you can verify the outcome later.
  • Make a result variable (with a value of 0) and an iterator ( set to the size of the number)
  • Create a while loop to go digit by digit through the number.
  • On each iteration, multiply the result by a digit raised to the power of the iterator value.
  • On each traversal, increment the iterator.
  • Compare the result value to a copy of the original number.
  • Print if the given number is a disarium number.
  • The Exit of the program.

Below is the implementation:

# Give the lower limit range as user input using the int(input()) function and store it in a variable.
lowlim_range = int(input("Enter some Random number = "))
# Give the upper limit range as user input using the int(input()) function and store it in another variable.
upplim_range = int(input("Enter some Random number = "))
print('The disarium numbers in the given range',
      lowlim_range, 'and', upplim_range, 'are:')
# Loop from lower limit range to upper limit range using For loop.
for m in range(lowlim_range, upplim_range+1):

    # given number
    num = m
    # intialize result to zero(ans)
    ans = 0

    # calculating the digits
    digit_s = len(str(num))
    # copy the number in another variable(duplicate)
    dup_numbr = num
    while (dup_numbr != 0):
        # getting the last digit
        remaindr = dup_numbr % 10
        # multiply the result by a digit raised to the power of the iterator value.
        ans = ans + remaindr**digit_s
        digit_s = digit_s - 1
        dup_numbr = dup_numbr//10
    # It is disarium number if it is equal to original number
    if(num == ans):
        print(num, end=' ')

Output:

Enter some Random number = 1
Enter some Random number = 250
The disarium numbers in the given range 1 and 250 are:
1 2 3 4 5 6 7 8 9 89 135 175

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.