- 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")

11th Sep 2022, 9:32 AM
Sachin Patel
Sachin Patel - avatar
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
11th Sep 2022, 10:04 AM
Lothar
Lothar - avatar
+ 4
Can you explain what an armstrong number is?
11th Sep 2022, 9:36 AM
Slick
Slick - avatar
+ 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)*...
11th Sep 2022, 9:43 AM
Ausgrindtube
Ausgrindtube - avatar
+ 4
I've played around with the code. You also have an infinite loop because of your i//10 line.
11th Sep 2022, 9:53 AM
Ausgrindtube
Ausgrindtube - avatar