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
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)
Comments
Leave a comment