Skip to content
Advertisement

Something wrong with armstrong number java code

I wrote a code for school in java that verifies if a number is an armstrong number. I programmed it so that it runs as many times until the user inputs 0, at which the program will terminate. I am having 2 problems.

  1. The code only works the first time through, if the user inputs 371 (an armstrong number) the first time, it works, but after that it returns that the number is not an armstrong number.

  2. When the user inputs 0, it still shows the statement whether it is or is not an armstrong number, which I don’t want it to.

This is the code:

JavaScript

Advertisement

Answer

Change your code so that it breaks immediately after entry of the userNum

e.g.

JavaScript

then you can also change your loop to a endless loop

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement