+ 3

(SOLVED)I need help

My code outputs all the perfect numbers within the given range. But it outputs "14" which is not a perfect number. I can't find why. Any help will be appreciated.( Look up "perfect number" in Google if you needed) https://code.sololearn.com/cD9V5XCvuHM9/?ref=app

7th May 2021, 3:38 PM
Rishi
Rishi - avatar
3 Answers
+ 2
You increment j after you assign a value. Therefore j is 1 too high. J always points to the next array position, which is unassigned. You need to decrement j once after your for loop so it points to the last valid integer. Also, you should define your array as a long int array and int i should also be a long int as well. Once you get into higher numbers, those will fail if they are ints.
7th May 2021, 4:25 PM
Jerry Hobby
Jerry Hobby - avatar
+ 2
Line 33 in the for loop change it from For(; j>=0; --j) To For(--j; j>=0; --j) (If you want to print them out at least)
7th May 2021, 3:53 PM
Odyel
Odyel - avatar
+ 1
Oh yes thank you Odyel and Jerry Hobby
8th May 2021, 3:32 AM
Rishi
Rishi - avatar