Home > Back-end >  Program in a circle about the daffodil number is the sum of variable scope
Program in a circle about the daffodil number is the sum of variable scope

Time:02-06

#include
#include
Int main ()
{int I, temp, RMD, sum;
For (I=100; i<=999; I++)
{temp=I; Sum=0;
While (temp> 0)
{RMD=temp % 10;
The sum +=pow (RMD, 3);
Temp=temp/10;
}
If (sum==I)
{printf (" % d \ n ", I);
}
}
}
This is a daffodil number of cycle structure program, including variable sum, if outside the large cycle definition sum=0, the result is wrong, why want to put the variable sum=o assignment in systemic circulation, is not very good? Strives for the big help, thank you very much,

CodePudding user response:

Sum in mai function in the body, the scope of the assignment of sum=0 here, has nothing to do with its scope, the sum is calculated for each number three cubic and (sum=153=1 5 ^ ^ 3 + 3 + 3 ^ 3), and then judge whether he is daffodil, so every time while in the end of the cycle, returned to the systemic circulation, when you enter a new number I judge, sum to zero, or brought the value of the last time the number of new judgment,

CodePudding user response:

 # include & lt; Stdio. H> 
#include
Int main ()
{int I, temp, RMD, sum;
For (I=100; i<=999; I++)
{temp=I; Sum=0;/temp/every time into the circulation, here the sum must be to assignment
While (temp> 0)
{RMD=temp % 10;//get the temp single-digit
The sum +=pow (RMD, 3);//a cubic
Temp=temp/10; 10 times//temp narrow
}
If (sum==I)
{printf (" % d \ n ", I);
}
}
}

CodePudding user response:

Because in the loop, so that each loop will sum=0; If placed outside the circle, then all the for only one time sum=0; All of the circulation in the sum is the sum of the last cycle value, rather than starting from 0,

CodePudding user response:

Because your sum is used to determine the number is the number of daffodils, if put on the outside and the inside not only accurate and reasonable to determine the first number is (100), the recycling is to calculate sum the bigger

CodePudding user response:

reference 1st floor QZJHJXJ response:
sum in mai function in the body, the scope of the assignment of sum=0 here, nothing to do with its scope, the sum is calculated for each number three cubic and (sum=153=1 5 ^ ^ 3 + 3 + 3 ^ 3), and then judge whether he is daffodil, so every time while in the end of the cycle, returned to the systemic circulation, when you enter a new number I judge, sum to zero, or brought the value of the last time the number of new judgment,
thank you, speak very clearly, I understand
  • Related