Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why my python Armstrong number code doesnt work

So ive done an armstrong number code using python and it works fine till the sum of individual digits but after that it doesnt work i will type the code and send a screenshot to show in picture.

number = int(input('Enter a number'))
n = len(str(number))
m = s = 0
list1 = []
for i in range(n):
    m = number % 10
    print(m)
    list1.append(m)
    number = number // 10
print(list1)

for o in list1:
    p = o ** n
    s = s + p
    print(s, 'is the sum of nth power of individual terms')

if int(s) == int(number):
    print('It is an armstrong number')
else:
    print('It is not an armstrong number')

enter image description here

I tried to program an armstrong number code for recreational purposes but it doesnt output the way i want it

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try this

number = int(input('Enter a number: '))
original_number = number
n = len(str(number))
s = 0
list1 = []
for i in range(n):
  m = number % 10
  list1.append(m)
  number = number // 10

for o in list1:
  p = o ** n
  s = s + p

if s == original_number:
  print('It is an Armstrong number')
else:
  print('It is not an Armstrong number')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading