Answer to Question #229921 in Python for srikanth

Question #229921
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 input3 
10
15
sample output 3
-1

please provide correct output in sample output 2
1
Expert's answer
2021-08-28T06:09:21-0400
A=int(input())
B=int(input())


flag = False
for i in range(A, B+1):
    x = len(str(i))
    sum1 = 0
    temp = i
    while temp > 0:
        digit = temp % 10
        sum1 += digit ** x
        temp //= 10
    if i == sum1:
        flag = True
        print(i,end=" ")
if not flag:
    print(-1)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Vunnam Indira
15.01.23, 19:22

Good answer

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS