Page 1 - RECURSION
P. 1
RECURSION
Recursion is a technique in which a problem is solved in terms of itself by repeatedly applying the
same procedure(s) to reduce it successively into smaller problem.
For solving smaller problem of its own type in Recursion a function calls itself directly or indirectly.
Sample program for
Printing a message forever
Without Recursion With Recursion
Method using Recursion for
Sum of first n natural numbers
sum(n) Program with output
=n + sum(n-1)
=n + (n-1) + sum(n-2)
=n + (n-1) + (n-2) + sum(n-3)
.
.
.
.
==n + (n-1) + (n-3) + (n-4) + ............... + sum(1)
sum(1) is the terminating condition and will return 1
Program using Recursion