- 1
Please anyone find my code error
i=int(input("enter number ")) sum=0 a=i while (i>0): sum=sum+(i%10)*(i%10)*(i%10) i//10 if a==sum: print("this is a armstong no") else: print("number is not Armstrong no")
4 Antworten
+ 6
Sachin Patel ,
the calculation of an armstrong number is not as it should be. the short definition of what an armstrong number is:
> an armstrong number is an integer such that the sum of the cubes of its digits is equal to the number itself. <
[EDITED]
to calculate the armstrong number we need these steps: ( using 407 as a sample which is an armstrong number)
(1) separate the digits of the number 407
4
0
7
(2) take each of the digit an calculate:
4 > 4**3 = 64
0 > 0**3 = 0
7 > 7**3 = 343
------------------------
sum: 407
+ 4
Can you explain what an armstrong number is?
+ 4
I think you want your if statement out of the while loop, right?
So you'd need to change the indentation.
You could also possibly write the sum line like this:
sum += (i%10)*...
+ 4
I've played around with the code. You also have an infinite loop because of your i//10 line.