0

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;

             return total += OddSum(low,up,total+low);

         }
4
Contributors
6
Replies
18
Views
2 Years
Discussion Span
Last Post by jwenting
Featured Replies
  • 1

    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. Read More

0

I Made it the solution is :

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


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


     }
0

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

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

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.

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.