title: Calculate Factorial using Lambda Expressions
body: <p>I am a beginner in java trying to find out if there is a way to calculate factorials using lambda expressions. In other words, what i need to do is modify the fatt operator so that the program outputs the factorial number.</p>
  
  <pre><code>import java.util.Stack;
  
  public class TestOperator {
  
  
      public static void main(String[] args) {
  
  
          Stack&lt;Operator&gt; expression = new Stack&lt;&gt;();
  
          expression.push(s -&gt; 2);
          expression.push(s -&gt; 3);
          expression.push(s -&gt; 1);
          expression.push(s -&gt; s.pop().apply(s) + s.pop().apply(s));
          expression.push(s -&gt; s.pop().apply(s) * s.pop().apply(s));
  
          System.out.println(expression.pop().apply(expression)); // prints (1 + 3) * 2 = 8
  
  
          // rewrite the operator **Fatt** in order to calculate the factorial
  
  
          Operator fatt = (s -&gt; { 
  
              s.pop().apply(s);
  
              return 42;
  
          });
  
          expression.push(s -&gt; 2);
          expression.push(s -&gt; 3);
          expression.push(s -&gt; 1);
          expression.push(s -&gt; s.pop().apply(s) + s.pop().apply(s));
          expression.push(fatt);
          expression.push(s -&gt; s.pop().apply(s) * s.pop().apply(s));
  
          System.out.println(expression.pop().apply(expression));
          // it should print fatt(1+3)*2 = 48
  
      }
  }
  </code></pre>
  
  <p>I think i should use the for loop, but i don't know how i should apply it using lambda expressions. The exercise says that it can also be done using recursion.</p>
  
  <p>Any help will be helpful</p>
  
  <p>Thanks</p>