Académique Documents
Professionnel Documents
Culture Documents
Recursive function
Created By:
Amanpreet Kaur &
Sanjeev Kumar
LPU CSE101 C Programming SME (CSE) LPU
Outline
Recursion
Examples of recursion
Finding factorial of a number
Finding Fibonacci series up to nth term
Recursion Vs Iteration
Values returned
For 5!, we write 5! = 5 * 4! 120
Then for 4!, 4! = 4 * 3!
24
Then for 3!, 3! = 3 * 2!
Then for 2!, 2! = 2 * 1! 6
2
Then for 1!, 1! = 1 * 0!
1
And if its comes to 0,
0!=1
Solve base case (1! = 0! = 1)
f( 3 )
return f( 2 ) + f( 1 )
return f( 1 ) + f( 0 ) return 1
return 1 return 0
Enter an integer: 0
Fibonacci( 0 ) = 0
or
Enter an integer: 1 output
Fibonacci( 1 ) = 1
or
Enter an integer: 20
Fibonacci( 20 ) = 6765