Home > Back-end >  Bosses for help.
Bosses for help.

Time:11-20

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

CodePudding user response:

refer to the second floor small farmhouse classmates reply:
you put 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 didn't add to the

Thank you understand, .

CodePudding user response:

reference 1/f, a white steamed bun 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 reference ~

Wonderful!!!!!!

CodePudding user response:

Put the I and the comparison of the sum in the second loop and outside, because the second cycle is to calculate all factors and, in it has not yet been computed to compare,
  • Related