Answer to Question #230262 in Python for Anilkumar Dakarapu

Question #230262

in the above case only armstrong numbers can be printed.For,Example consider the two number that is "A" value should be 10 and "B" value should be 15. There is no armstrong number between these two intervals so we need to print a"-1".but the output should be shown empty value.how could we print -1 please explain that code


1
Expert's answer
2021-08-27T17:45:52-0400
A = int(input())
B = int(input())

def is_armstrong(x):
    degree = len(str(x))
    _sum = 0
    for i in str(x):
        _sum += int(i)**degree
    if _sum == x:
        return True
    return False

# flag to output -1 if there are no armstrong numbers    
is_armstrong_exist = False  
for i in range(A, B + 1):
    if is_armstrong(i):
        is_armstrong_exist = True
        print(i)
        
if not is_armstrong_exist:
    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

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS