title: Recursion Program
body: <p>I am stuck up in this code: </p>
  
  <blockquote>
    <p>Problem: A child can hop a staircase of steps n in 1,2 or 3 steps at one time. Given a value of n, print all the permutations of the order in which he can climb the staircase.</p>
  </blockquote>
  
  <p>This is my code: </p>
  
  <pre><code>    public class HoppingLad
      {
          int count;
          void hop(int n,int present)
          {
              if(n==present)
              {
                  count++;
                  System.out.println("\nFinished type "+count+" climbing.\n");
              }
              else
              {
                  if((n-present)&gt;=1)
                  {
                      System.out.print("\nClimbed 1 step.\nReached "+(present+1)+"   ");
                      hop(n,present+1);
                  }
                  if((n-present)&gt;=2)
                  {
                      System.out.print("\nClimbed 2 step. \nReached "+(present+2)+"   ");
                      hop(n,present+2);
                  }
                  if((n-present)&gt;=3)
                  {
                      System.out.print("\nClimbed 3 step. \nReached "+(present+3)+"   ");
                      hop(n,present+3);
                  }
  
  
              }
  
          }
  
          public static void main(String [] args)
          {
              HoppingLad hl=new HoppingLad();
              hl.hop(3, 0);
              System.out.println("There are "+hl.count+" ways to climb.");
          }
      }
  </code></pre>
  
  <p>The output is : </p>
  
  <pre><code> Climbed 1 step.  
   Reached 1  
   Climbed 1 step.  
   Reached 2  
   Climbed 1 step.  
   Reached 3   
   Finished type 1 climbing.
  
  
   Climbed 2 step. 
   Reached 3   
   Finished type 2 climbing.
  
  
   Climbed 2 step. 
   Reached 2   
   Climbed 1 step.
   Reached 3   
   Finished type 3 climbing.
  
  
   Climbed 3 step. 
   Reached 3   
   Finished type 4 climbing.
  
   There are 4 ways to climb.
  </code></pre>
  
  <p>The output I get is partly correct, partly incomplete. The number of ways to climb the staircase is correct but as you notice, </p>
  
  <blockquote>
    <p>Climbed 2<br>
    Reached 3</p>
  </blockquote>
  
  <p>part of the output is coming as it is without </p>
  
  <blockquote>
    <p>Climbed 1<br>
    Reached 1 </p>
  </blockquote>
  
  <p>part coming before it. I have drawn the recursion tree and the tree even suggests that the first part is not there in the output. </p>
  
  <p>However, the user has to be instructed from the ground level. I have tried many things, to no avail. Can anyone fix this out for me please?</p>