Abel.S
Abel.S

Reputation: 19

Condition for Armstrong number

Can anybody tell me what is wrong with my code for checking whether a number is an Armstrong number?

n=input('Enter the number=')
m=n
s=0
while n>0:
    d=n%10
    s=s+d**3
    n=n*10
if m==s:
    print'The number is an Armstrong number'
else:
    print'The number is not an Armstrong number'

Upvotes: 0

Views: 925

Answers (2)

Abel.S
Abel.S

Reputation: 19

I got the program to work eventually. Turns out I had kept typing the statement n=n/10 as n=n*10. Mistakes do happen sometimes:)

n = input('Enter the number=')
m = n
s = 0
while n>0:
    d = n%10
    s += d**3
    n /= 10
if m==s:
    print'The number is an Armstrong number'
else:
    print'The number is not an Armstrong number'

Upvotes: 1

Rohan Chavan
Rohan Chavan

Reputation: 1

sum = 0
no = int(raw_input("Enter the the number to check it is armstrong on or not :"))
pow_no = len(str(no))
check = no

print "#################Method -I Result##############"
while 0<no:
  sum = sum +((no%10)**pow_no)
  no = no / 10
if check == sum:
  print "%s is Armstrong"%check
else:
  print "%s is not Armstrong"%check

print "################Method -II Result#############"
for i in str(no):
  sum = sum + (int(i)**pow_no)
if check == sum:
  print "%s is Armstrong"%check
else:
  print "%s is not Armstrong"%check

Upvotes: 0

Related Questions