Escolar Documentos
Profissional Documentos
Cultura Documentos
Any function which calls itself is called recursive. A recursive ethod solves a
problem by calling a coy of itself to work on a smaller problem
Example-Factorial problem n!=nX (n-1)!= nX (n-1) X (n-2) !
Here the factorial of n is resolved by calling the factorial function but a siler one
for (n-1).
But this will end once we reach 1 ie we donot further call factorial function.
Thus it is important that the recursion terminates at a certain stage or we will
end up in a infinite loop.
Why recursionCampared to iterative code recursion code is shorter and easier.But all recursive
problems can be solved through iteration (looping).
Not all roblems can be resolved through recursion the mai one being the
problem can be solved by reducing it to a simler version of itself.
Another disadvatnge of recursion is extra memory requirement , because each
time the function is called (by itself) a new copy of the function is loaded on the
RAM.
/* Program to calculate factorial through recursion
#include<stdio.h>
Int fact(int)
Void main()
{
Int n;
Printf(Enter an integer to calculate n! \n);
Scanf(%d,&n);
Printf(Factorial of n is %d,fact(n));
}
Int fact(int N)
{
Int result
If(N==1)
Return 1;
Else
Result= N*fact(N-1)
Return (Result);
}
Now suppose the problem is to find sum of first n natural nubers, how will the
program look like?
Void main()
{
Int n;
Printf(Enter an integer to calculate sum of first n natural numbers \n);
Scanf(%d,&n);
Printf(Sum of first n natural numbers is %d,sum(n));
}
Int sum(int N)
{
Int result
If(N==1)
Return 1;
Else
Result= N+sum(N-1)
Return (Result);
}
The program is vey similar to factorial example discussed above
Next we will see a program which will print Fibonacci series using recursion.
Fibonacci series is 1 1 2 3 5 8 13 21 34 55 89 143.
First 2 numbers is 1 but rest of the series is sum of previous 2 numbes ie 2=1+1,
3=2+1, 5=3+289=55+34
Suppose we need to find 5th term of Fibonacci series written as Fib(5)
Fib(5)=Fib(4)+Fib(3)
But Fib(4)=Fib(3)+Fib(2)
Fib(3)=Fib(2)+Fib(1)
Now we know Fib(1) and Fib(2) are 1 so for solving
Fib(5) we convert it as
Fib(5)=Fib(4)+Fib(3)=Fib(3)+Fib(2) +
Fib(2)+Fib(1)=Fib(2)+Fib(1)+Fib(2)+Fib(2)+Fib(1)=
1+1+1+1+1=5, This logic can be implemented as a rogra to print the nth eleent
of the Fibonacci series
/* Program to print nth eleent of the Fibonacci series
#include<stdio.h>
Int Fib(Int)
Void main()
{
Int n;
Printf(Enter the element number of the Fibonacci series \n);
Scanf(%d,&n);
Printf(The elemt of Fibonacci series is %d \n,Fib(n));
}
Int Fib(int N)
{
If (N==1 || N==2)
Return 1;
Else
Result=Fib(N-1)+Fib(N-2);
Return (Result);
}
Supose n=7 , we need to calculate Fib(7)
By recursion Fib(7)=Fib(6)+Fib(5) ie 2 child functions are created in this case
Return GCD(M%N,N);
If((N>M)&&(N%M==0))
Return M;
Elseif ((N>M)&&(N%M !=0))
Return GCD(M,N%M);
}
Now let us consider a rogram which will print all perutations of the string entered
by the user hrough recursion. Meaning if user enters the string ABC the rogra ust
output ABC,ACB , BAC, BCA ,CAB, CBA.
First let us see how the problem can be convered to recursive logic-
In first level I have swapped the first eleent with the eleents after it (2 nd, 3 rd
etc)
In level 2 I have swaped the second elent with the eleent after it (3 rd,4th etc)
In level 3 I have swapped the third elemt with the eleent after it (4 th eleent)
AB
I0 n1 j0
AB
i1n1 AB
I0 n1 j0
AB
I0 n1 j1
BA i1n1 BA
I0 n1 j1
Ab
ABC
Void main()
{
Char A[4]
A[4]=\0;
Genbinary(3,A);
}
?
N=3
{
If(n<1)
0
pRintf(\n %s,A);
else
{
A[n-1]=0;
N=3
N=2
N=1
Genbinary(n-1,A);
A[n-1]=1;
Genbinary(n-1,A);
}
}
N=3
N=2
N=1
N=1
N=2
N=2
N=1