Find a great god help me this small white kangkang I this program is what went wrong, title: output all the perfect number of less than 1000, All perfect number: if a number of factors (excluding the number itself) is equal to the sum of the number, this number as the perfect number, such as "6", all factors for,2,3,1 1 + 2 + 3=6, it is perfect number 6, My result always appear some error data, such as 24,
CodePudding user response:
int I, j, sum; For (I=2; i <=1000; I++) { sum=0; For (j=1; J & lt;=I/2; J++) { If (I % j==0) { The sum +=j; } } If (sum==I) { Printf (" % d \ n ", I); } }
For your reference ~
CodePudding user response:
You put the sum==I judge on j loop, I finished to calculate all the factors, such as judgment, otherwise such as 24, factor 1,2,3,4,6,8,12 before 6 items together just 24, 12 added