Armstrong numbers between two intervals
Write a program to print all the Armstrong numbers in the given range
A to B(including A and B). An N-digit number is an Armstrong number if the number equals the sum of the Nth power of its digits.
Input
The first line has an integer
A. The second line has an integer B.
Output
Print all Armstrong numbers separated by a space.
If there are no Armstrong numbers in the given range, print
-1.
Explanation
For
A = 150 and B = 200
For 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
Sample output 3
200
Sample input 3
-1
try:
a = int(input('Enter number A: '))
b = int(input('Enter number B: '))
if a < b:
numbers = []
for num in range(a,b + 1):
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
if num == sum:
numbers.append(num)
if len(numbers) != 0:
for i in numbers:
print(i, end=' ')
else:
print('-1')
else:
print('A should be less than B')
except ValueError:
print('A and B must be integers')
Comments
Leave a comment