# Armstrong Number
num= int(input("Enter a number: "))
a=[]
b=[]
while num>0:
digit=num #Taking the last digit
a.append(digit) # creating a list with the individual digits
num=num//10
for i in a:
numb=i**len(a) #Calculating the power
b.append(numb) # Creating a new list with the numbers powered to length of the digits
summ=sum(b) #Sum of all the digits in the new list "b"
print("Sum is:", summ)
if summ == num:
print("Yes Armstrong No")
else:
print("Not Armstrong")
The last summ==num "if" condition is always returning the else condition.
Eg if my number (num) is 371, then 3^3 7^3 1^3 is also 371 which is original number = sum and hence it's an Armstrong number so it should return "Yes Armstrong No" but it returning "No" (else condition)..I am unable to identify the error as summ==num (is true here).
CodePudding user response:
You are overwriting the number.
You can just write:
num = int(input("Enter a number: "))
sum = 0
for digit in str(num):
sum = int(digit)**len(str(num))
if sum == num:
print("Yes Armstrong No")
else:
print("Not Armstrong")
And it will work.
CodePudding user response:
You should make a copy of your original input:
original_num = int(input("Enter a number: "))
num = original_num
... and check that instead, as you overwrite num
a few times:
if summ == original_num: