title: Factorial using Recursion in Java
body: <p>I am learning Java using the book Java: The Complete Reference.
  Currently I am working on the topic Recursion.</p>
  
  <p><strong>Please Note:</strong> There are similar questions on stackoverflow. I searched them but I didn't find the solution to my question. I am confused with the logic in the following program.</p>
  
  <p>If I run the below program, it produces the correct output, but I didn't understand the logic.</p>
  
  <ul>
  <li>I didn't understand the logic in the following line : <strong>result = fact(n-1) * n;</strong></li>
  <li>From my knowledge, If we pass the value of n=4 as shown in the below program, </li>
  <li>Then, 3 * 4 is stored in the result i.e., 12. </li>
  <li>Again, fact(n-1) is called. Then n becomes 3.</li>
  <li>Then the 2 * 3 is stored in the result replacing the previous 12.</li>
  <li><p>I think you understood where I am stuck up/confused.</p></li>
  <li><p>Thank you.</p></li>
  </ul>
  
  <blockquote>
  <pre><code>class Calculation
  {
      int fact(int n)
      {
          int result;
  
         if(n==1)
           return 1;
  
         result = fact(n-1) * n;
         return result;
      }
  }
  
  public class Factorial
  {
       public static void main(String args[])
       {
         Calculation obj_one = new Calculation();
  
         int a = obj_one.fact(4);
         System.out.println("The factorial of the number is : " + a);
       }
  }
  </code></pre>
  </blockquote>