Python program that uses a for loop and the is_disarium()
function to print all Disarium numbers between 1 to 100:
def is_disarium(n):
num = n
digits = [int(d) for d in str(n)]
length = len(digits)
sum = 0
for i in range(length):
sum += pow(digits[i], i+1)
return sum == num
for i in range(1,101):
if is_disarium(i):
print(i)
This program uses a for loop to iterate over the range 1 to 100, and for each number, it checks if it is a Disarium number by calling the is_disarium(n)
function. If the function returns True
, it prints the number.
The function is_disarium(n)
is defined earlier as a helper function that takes an integer as an argument, it converts the integer to a list of its digits using list comprehension and the int(d)
function. It then calculates the sum of the digits powered with their corresponding position using a for loop. It compares the sum with the original number and returns a boolean value indicating if the number is a Disarium number or not.
This program will print all the Disarium numbers between 1 to 100, which are 1, 2, 3, 4, 5, 6, 7, 8, 9, 89.