Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Recursion

By:Dr. P.S.Tanwar
Recursion
Recursive functions
A function who call itself is called
recursive function.
Two parts are required in Recursive Function

• Recursive function

• Terminating condition

By:Dr. P.S.Tanwar
Recursion
Recursive functions
If a terminating condition is missing then
it will run in infinite loop.

Recursive function works in LIFO


(Last In First Out) form

By:Dr. P.S.Tanwar
Recursion
Recursive functions

In recursive function a big problem


is solved by breaking it in to a
smaller instances of similar type.

By:Dr. P.S.Tanwar
Q/A
Recursion is a method in which the solution
of a problem depends on ____________
A) Larger instances of different problem

B) Larger instances of the same problem

C) Smaller instances of the same problem

D) Smaller instances of different problems

By:Dr. P.S.Tanwar
Factorial
4!=4x3x2x1=24 n! =n.(n-1)!
5!=5x4x3x2x1=120
fact(n)=n* fact(n-1)

5!=5x4!
=5x4x3!
=5x4x3x2!
=5x4x3x2x1!
=5x4x3x2x1

By:Dr. P.S.Tanwar
Factorial
Recursive Function (for all n>1)
n! =n.(n-1)!

Terminating Condition(for n=1)


1!=1

By:Dr. P.S.Tanwar
Factorial
Recursive Function
factorial(n)=n*factorial(n-1)

Terminating Condition
• factorial(1)=1 n! =n.(n-1)!
Or
fact(n)=n* fact(n-1)

By:Dr. P.S.Tanwar
Factorial
1 if n=1

n! =

n.(n-1)! otherwise

1 if n=1

factorial(n)=

n*factorial(n-1)otherwise
By:Dr. P.S.Tanwar
Recursive Function
Factorial
int factorial (int n)
{
if(n==1)
{
return 1;
}
else
{
return n * factorial(n-1);
}
}

By:Dr. P.S.Tanwar
Recursive Function
Recursive function call(Factorial)
factorial(5) 120

5 * factorial(4) 24

4* factorial(3) 6

3* factorial(2) 2

2* factorial(1) 1
120 Output
5!=120

By:Dr. P.S.Tanwar
Q/A
Recursion is similar to which of the
following?

A) Switch Case

B) Loop

C) If-else

D) if elif else

By:Dr. P.S.Tanwar
Q/A
What will happen when the given code
void rec()
{ snippet is executed?
rec();
} A) The code will be executed successfully and no output
int main()
{ will be generated
rec();
return 0; B) The code will be executed successfully and random
} output will be generated

C) The code will show a compile time error

D) The code will run for some time and stop when the
stack overflows
By:Dr. P.S.Tanwar
Fibonacci Series
Fibonacci Series

0,1,1,2,3,5,8,…

By:Dr. P.S.Tanwar
Fibonacci Series
Terminating Condition(if n=1 or
n=2)
Fibo(n)=0 if n = 1

Fibo(n)=1 if n = 2

Recursive Function (otherwise)


Fibo(n)=Fibo(n-1)+Fibo(n-2)
By:Dr. P.S.Tanwar
Fibonacci Series

1. Terminating Condition(if n=1 or


if(n=1)
Fibo(n)=0
if(n=2)
Fibo(n)=1

2. Recursive Function (otherwise)


Fibo(n)=Fibo(n-1)+Fibo(n-2)

By:Dr. P.S.Tanwar
Fibonacci Series
Fibo(1)
fibo(1)

Fibo(2)
fibo(2)

By:Dr. P.S.Tanwar
Fibonacci Series
Fibo(3)
1
fibo(3)
1
0+1=1
fibo(1) + fibo(2)
0 1

fibo(1) 0 fibo(2) 1

0 1

By:Dr. P.S.Tanwar
Fibonacci Series

By:Dr. P.S.Tanwar
Q/A
What is the output of the following
void rec(int n)
{
code? if(n == 0)
return;
A) 10 cout<<n<<" ";
rec(n-1);
}
B) 1 int main()
{
rec(10);
C) 10 9 8 … 1 0 return 0;
}
D) 10 9 8 … 1

By:Dr. P.S.Tanwar
Recursive Member
Function

By:Dr. P.S.Tanwar
Recursive Member
Function

120

m1 n Output
5 Please enter value of n: 5
Factorial of 5 is 120
int factorial(int n) { f
} 120

By:Dr. P.S.Tanwar
Tower of Hanoi
Task: Move rings from ‘L’ Peg to ‘R’ Peg
Move one ring at a time from top side

Don’t move large ring over smaller one

L C R

By:Dr. P.S.Tanwar
Tower of Hanoi

Step 1 If n==0 stop


Step 2 Otherwise
Step 2 (a) move (n-1) rings from ‘from’ to ‘temp’
Step 2 (b) move ring n from ‘from’ to ‘to’
Step 2 (c) move (n-1) rings from ‘temp’ to ‘to’

L C R

By:Dr. P.S.Tanwar
Tower of Hanoi
#include <iostream> int main()
{
using namespace std; TOH(3,'L','R','C');
void TOH(int n, char from, char to, char temp) return 0;
{ }
if(n==0)
{
return ;
}
else
{
TOH(n-1,from,temp,to);
cout<<"Move ring "<<n<<" from "<<from<<" to "<< to<< endl;
TOH(n-1,temp,to,from);
}
}

By:Dr. P.S.Tanwar

You might also like