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
VulpesPosted Nov 11, 2011, 12:46 PM
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++)
Josip PavicPosted Nov 11, 2011, 1:07 PM
thanks
jonny111
Josip PavicPosted Nov 11, 2011, 12:41 PM
karthik vPosted Nov 11, 2011, 11:56 AM
VulpesPosted Nov 11, 2011, 10:06 AM
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.
Josip PavicPosted Nov 11, 2011, 9:48 AM
Datta KharadPosted Nov 11, 2011, 9:41 AM
You didn't mention your expected result (what do you want)? bcoz above code is working.