Im trying to add odd numbers recursively from n to m but its not working . im sure its in the recursive logic on how im passing the arguments or something.

`````` public static int OddSum(int low, int up,int total)
{
if(low==up)
{
return up;

}else
{

low+=2;

}
``````
4
Contributors
6
Replies
18
Views
4 Years
Discussion Span
Last Post by jwenting

I Made it the solution is :

``````public static int OddSum(int low, int up,int total)
{
int tot=total;
tot+=low;

if(low==up)
{
}else
{
low+=2;
return OddSum(low,up,tot);
}

}
``````

Is there any reason why you arent using a for loop?
Heres an example.

``````for(int i = low; i < up; i += 2)
{
low += 2;
}
``````

Because that's not a recursive solution?

but why go to the trouble of using a recursive solution when it can be done far more easily otherwise...
Very valid question if you ask me.

IK would imagine (with a high dehgree of confidence) that this was set as a training exercise in simple recursion.

probably. Which shows once again how pointless such assignments are when presented outside their context :)