Answer to Question #253314 in Python for Ranganath

Question #253314
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



please provide correct output in sample output 2



please provide correct output in sample output 3

Sample output 3 is
-1

the above output 3
A=10
And
B=15

Output is
-1
Please provide correct output of sample output 3
1
Expert's answer
2021-10-19T00:50:11-0400
def CheckArmStrongNumber(n):
    Flag=-1
    s = str(n)
    l = len(str(n))
    Sum =0
    for r in range(0,l):
        a = int(s[r])
        Sum = Sum + pow(a,l)
        if(Sum == n): Flag=1
    return(Flag)    


u = int(input("Enter lower range: "))
v = int(input("Enter upper range: "))
f=-1
s=""
for r in range(u,v+1):
    Flag = CheckArmStrongNumber(r)    
    if(Flag==1):
        f=1
        s = s + str(r) + " "
        
if(f==-1):
    print("-1")
else:
    print(s)








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

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS