Recursive functions in C

LakshmiSarvani1 277 views 8 slides Jun 10, 2020
Slide 1
Slide 1 of 8
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8

About This Presentation

Recursive functions in C


Slide Content

Session – 20 Recursive Function Session Outcome: Understanding Recursive Functions Examples Of Recursive Functions

Recursive Functions Recursive function is closely related to mathematical induction Recursive function is a function that calls itself Every Recursive function will have exit or stop condition – other wise the recursive function will keep on calling itself Saturday, May 16, 2020 For suggestions or queries contact [email protected]

Inherently recursive functions 5! 5*4! 4*3! 3*2! 2*1! 1 5! 5*4! 4*3! 3*2! 2*1! 1 Final value=120 1 2!=2*1=2 returned 3!=3*2=6 returned 4!=4*6=24 returned 5!=5*24=120 returned

Finding Factorial Recursively Saturday, May 16, 2020 For suggestions or queries contact [email protected] fact(5) 5*fact(4) 4*fact(3) 3*fact(2) 2*fact(1) 1 2 6 24 120

Finding Factorial Recursively   Saturday, May 16, 2020 For suggestions or queries contact [email protected] Corresponding Recursive Function int fact( int n) { if(n ==1 || n ==0) return 1; else return n*fact(n-1); }

Finding Factorial Recursively Complete program is: # include< stdio.h > int fact( int ); main() { int n; scanf ("% d",&n ); printf ("% d", fact (n)); } int fact( int n) { if(n ==1 || n ==0) return 1; else return n*fact(n-1); }

Sum of Natural Numbers Recursively Saturday, May 16, 2020 For suggestions or queries contact [email protected]   Corresponding Recursive Function int sum( int n) { if(n == 1) return 1; else return n + sum(n-1 ); }

Finding nth term in Fibonacci Series Recursively   Corresponding Recursive Function int fib( int n) { if( n ==0) return 0; if ( n ==1) return 1; else return fib(n-1)*fib(n-2); }