Recursion What is Recursion? Factorial Expression 1: - Factorial Expression 1:
- fact(n) = n * (n – 1) * (n – 2) * … * 1
- = n * (n – 1) * … * (n – (n –1))
- public int fact ( int n )
- {
- int f = 1;
- for ( int i = 1; i <= n; i++)
- f = f * i;
- // loop invariant: f = fact(i)
- return f;
- }
Do'stlaringiz bilan baham: |