I have tried to combine isPrime metohd with this program, but for some reason I am getting bunch of numbers from 1 to 9999. I don't know whether my thought process is bad or I might just overlooked something.
After looking at definition of a perfect number I've modified code, so that it looks like this, but again I am getting the wrong results.
I am getting the wrong results.
How are you debugging the code?
I suggest that you call the method with only a few values (not 1000) and look at the results of each expression to see where the code is going wrong. I use print statements to print out the values of all the variables as there values are changed and used. If you know what the values should be, seeing the actual values from the program will show you where the problem is.
1) Goob job using constants for HIGH_NUM and LOW_NUM
2) Classes should start with an uppercase letter, so the class should be named PerfectNumber2.
3) In small programs like this, it doesn't matter much, but in general you should avoid putting if statements on one line. So for instance,
Knute Snortum wrote: Good job!
Thanks! It means a lot to a new programmer. I will adopt your advises.
fred rosenberger wrote:I would argue your isPrime method is not correct. It will return false for the input value of "2"
I will just write one more if statement.
Fred Kleinschmidt wrote:Why include x in the loop if you are just going to subtract it later?
I've already did what you suggested, just I haven't posted here. But thanks anyway.