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);

         }

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);
         }


     }

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.