Well, yes...
public static int Sum(int start, int end)
{
if (start >= end)
{
return 0;
}
else
{
return Sum(start, end);
}
}
If you call Sum with start less than end:
Sum(1, 2);
Then it will look at the two values, and see which is the greater.
It's
end
, so the
if
test will fail, and it will execute the
else
clause ... which passes the same values to the Sum method so it does teh same thing again, and again, until it runs out of stack space and fails!
Probably, what you meant to do was
return start + Sum(start + 1, end);
But to be honest, a recursive method to do this is a bit overkill! :laugh:
Why not try it with a simple
for
loop?