In [1]:
n = 152
num = n
count = 0
while n != 0:
r = n % 10
count = count + 1
n = n // 10
n = num
sum = 0
while n != 0:
r = n % 10
sum = sum + r**count
n = n // 10
if sum == num:
print (num, ' is a armstrong number.')
else:
print (num, ' is not a armstrong number.')
152 is not a armstrong number.
In [3]:
a = int (input("Enter the first value of the range: "))
b = int (input("Enter the last value of the range: "))
if a > b:
a = a + b
b = a - b
a = a - b
print ("Armstrong numbers from ", a, " to ", b, " are: ")
for n in range(a, b + 1):
num = n
digits = 0
while num != 0:
digits = digits + 1
num = num // 10
num = n
sum = 0
while num != 0:
r = num % 10
sum = sum + r ** digits
num = num // 10
if sum == n:
print (n, end = " ")
Enter the first value of the range: 1 Enter the last value of the range: 1234567 Armstrong numbers from 1 to 1234567 are: 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834
No comments:
Post a Comment