Armstrong numbers between two intervals
Write a program to print all the Armstrong numbers in the given range
The first line has an integer
Print all Armstrong numbers separated by a space.
If there are no Armstrong numbers in the given range, print -1.
For
A = 150 and B = 200For example, if we take number 153, the sum of the cube of digits
1, 5, 3 is 13 + 53 + 33 = 153.
So, the output should be
153.
Sample Input 1
150
200
Sample Output 1
153
Sample Input 2
1
3
Sample Output 2
1 2 3
a = int(input())
b = int(input())
res = []
for i in range(a, b+1):
tmp = 0
str_num = str(i)
for el in str_num:
tmp += int(el)**len(str_num)
if int(tmp) == i:
res.append(i)
if len(res) == 0:
res.append(-1)
print(*res)
Comments
Leave a comment