Recursion
Recursion
in One Shot
What and n! ux(n
=
-
1)!
Why? f(x) nx
=
5) = Sx4x3x2x1
5. = 3x4!
4! = 4x3x2X(
u 4x3!
=
Function calling itself
factorial (n) = n* factorial (n-1);
int factorial (int n) [
n! n+ (n 1))
-
ba
3*2
5!= 5* a3!=
a n!= 4*3 a2!= 2*1
I I ⑨
2
24 6
Ques : Make a function which calculates the
factorial of n using recursion.
3
&
3 factorial (2);
u X
factorial (3);
2
2 factorial (1)
2
4y
n
=
1 factorial (0)
O
factorial (-1)
*
8
5
storal
co
I 2
& -
u*
al
(3):24
3 2
&
and
-
3 factorial (2);
2+onale
. . .
2 6
=
Fragram
120
fatu)24
->
fact(s) *S*
faL3)6:24
+
u
fo
6
*(2)2
=
d
1 = 2
*
2 f1)
↓
I
Ques : Print n to 1 using recursion
Output
I
int decreasing (int n) < 7
3
3
2
I
~ Output
x
-
· M
&
.
- .
.
·Good Morning
·
Good Morning
d
I
&
de
v r
· Good
Morning
v r · Good Morning
· or
3 O
N & ~ I · Good Morning
d A O
3 2
w v
c v
v
&
- ⑧
2
I
U
I v &
fun) >[
↑
base case
code
recursive call
I code
↑return
3
Ques : Print 1 to n
L Output-
I
out: I &
I
2 O
2
n 5 3
=
& 3
Y
·
Y
S
o S
g
6
Using extra parameter
:
Ques : Print 1 to n & parameterised)
Output
I
O I
15
·
2
·
3
· U
·5
n 5 ⑧
=
S
Ques : Print 1 to n (after recursive call)
O Output
I
U / *
. . . . · I
-
3
. . . &
⑥ - *
2
r
v
& I p ⑧ 3
v
↓ 3
r
o
-
O · Y
-
v
X
v
⑧ 2
2 ↑
w /
& - o I
ve
v
C
#work: Print Increasing
Decreasing a
-
n 4 ->
Y
=
Call
#
tnt: sepeble, call be bord
3e DRY RON
HW n
=
n Y,s
=
0
=
I
Y O & O 10
~
v v v &
1 O
i
v ~
= =
y - -
v -
&
3 Y O 10
⑳ o &
-
& v
. . . . .
Are 2
&
U 7
- -
- r
v -
-
-
N 24 + 3 v I a
&
- ①
- & v
Ques : Print sum from 1 to n (Return type)
(n) n* factorial (n-1);
factorial =
sum(n) n
=sum(n +
-
1);
sum(s) 1 + 3 4 5 5+
sum(Y)
=
=
2+ + +
u + Sum (3)
d
3 + sum (2)
↓
suml
2+
Ques : Make a function which calculates ‘a’
raised to the power ‘b’ using recursion.
ab
int power =1; axaxaxa...
=
inta, b; b times
i + +<
for (inti =
1; <b; i
if (b 0) return 1:
power-powera;
=
=
a* power (a.b-1);
power (a,b)
=
z
b * b
-
1
a : a a i
2 22'
=
i c+ y
->
=
-
=
Y
2 -3 I
*Multiple Calls
Ques : Write a function to calculate the nth
fibonacci number using recursion.
1 1235813213455 89...
1234567 89 &D 1)
fibo (n) =
fibo (n-1) + fibo (n-2);
if (n 1 on = 2) return I
=
=
=
A
(n =)((n 2)
-fibo(s)
= =
=
Head
return I
-
↓ d
fibo(2) fibe
+
Ques : Stair Path - I I I 1
111 2
+U S
N stair
1211
single step, double step Y
1128
no. of ways, so that 21 1 1
3
nth
the person reaches
122
stair.
2 212
221
n + n
-
1,n -
2
My 1
X
g 5 steps
Ques : Stair Path -
2 1 1 1 1
Tw 112
I 21
+U
N stair
or
2 ↑ I
a
·inscot tablested
22
13
31
stair.
M
1
g
Ques : Power function (logarithmic)
pow (a,b) pow(a,b(z)
b
aabr; it (b 0) return 1;
=
a
=
=
=
-
por(a,b/2)
I
ab b12*able;
I
a
26 2263
=
=
233232
64
63
2*262 2
-
2
=
32
6
64 + 16
&
2 =
2 2
calls
: calls
2
16
= 28 + 28
&
U Y
28
↑
*
2 2
2*20
=
2
=
2
2
U
=
2
* 22
2 I * I
2
I
2 2
it (b == 1) return a j
Ques : Power function (logarithmic)
a
=aPl2x abl2 #Station:
if bis even
#Problems
m
a
= abl2xabl2
7
2
272x512
-
2
if bis odd
C 27 = 2
3 x 23 = 16 ab aP2aP12x
=
a
**
*
b S
=232x312
=
2 a
= a312xa32x a
623 2
= x 2
&
=
a
"x
w
a x a
2= 2x2 = Y
29 =
2 + 2" +
2;
2
=
x(*;
Ex 2
2
2 =
2 =
20x 20 =2;
T
a 2
=
b 9
=
b + b(z +
b/a+b81
b
a # b calls
2
C
Co
=
n terms
b)-log
⑤an an = a,x2"
=
8 n- 1
a
s
= 1 b 1x2
=
v z -b
ce
=
=
***
no.
of ways
Ques : Maze path ->
Down, Right
1 a time'
'I stepat
n,m
3
:po
1 2
1
& DDRR
DRDR
2
DRR D DRR
3 RRDD MRD
- RDR
R DRD
RDD R
#
i/apR
Ques : Maze path n 3,m 3
= =
if (n = 1
=
2&m =
1) return
=
1
- if (n 1) Ican't go down
=
rightways +=
3
I 1
a
on
lot 2
1222
100
2222
0 1
1
&
Call Stack -
am
to
I
-actcul
-main
↑
MI
:El
·
·
Pre
In
Post 1
Post 2
2
1
Pre In Post Output
↑
I
Tree Diagram · Pre2
cy/dic vrypcp(2) ·
Dre 1
· In 1
Sofo
pip (1) post 1 · Post 1
· In2
·
Pre 1
· In 1
·
Post 1
·
Post2
Ques : Print zig-zag
Input Output
1 111
2 211121112
3 321112111232111211123
4 432111211123211121112343211121112321112111234
x &
#& s
I
A - C
A B
-
c + B
Input en -n
of disks
+ C
A
disks min moves
33 33 -
1 7
= 1 - A
y-2"
3
-
1 19
=
3 - 23 -
1 31
=
Ques : Tower of HANOI
#
As B
A - C
B C +
n 2
=
#!
m. moves - 12 -
1 3
=
Ques : Tower of HANOI n-/
disks
↑
small pyramid, calle
1
s6Gestimation
S-> H
S A
-
largest disk
H - A small
pyramid, call
Ques : Tower of HANOI