I now you would find this funny, but my problem is for loop. MessageBox display wrong result. cant figure out why??
int loopStart;
int loopEnd;
int answer = 0;
loopStart = int.Parse(textBox1.Text);
loopEnd = int.Parse(textBox2.Text);
for (int i = loopStart; i < loopEnd; i++)
{
answer = answer + i;
}
MessageBox.Show(answer.ToString());
Thank for your help.
Jonny111
Loading

VulpesRank #55 · 29.2K reputationPosted Nov 11, 2011, 7:46 AM EST
If you want to include the endpoint of 10 (so the total is 55), then change this line:
for (int i = loopStart; i < loopEnd; i++)
to this:
for (int i = loopStart; i <= loopEnd; i++)
Datta KharadRank #2,135 · 668 reputationPosted Nov 11, 2011, 4:41 AM EST
You didn't mention your expected result (what do you want)? bcoz above code is working.
Josip Pavic20 reputationPosted Nov 11, 2011, 4:48 AM EST
VulpesRank #55 · 29.2K reputationPosted Nov 11, 2011, 5:06 AM EST
If it were "1" and "11" respectively, then the result would be 55.
It might be to do with the fact that you're excluding the endpoint in the summation.
karthik v115 reputationPosted Nov 11, 2011, 6:56 AM EST
Josip Pavic20 reputationPosted Nov 11, 2011, 7:41 AM EST
Josip Pavic20 reputationPosted Nov 11, 2011, 8:07 AM EST
thanks
jonny111