When a user defined function
perform a routine for certain time with the help of any iteration but by
calling itself, the function is known as recursive function.
Recursions are easy to handle
and make the program very simple than its iterative solution, which is very big
and complex but
it hurt both the complexity, time and space.
Advantages :
While writing a program using
multiple iteration make the program very complex and big while recursion is
usually simple.
Disadvantages :
1. Recursive solution
is always logical and it is very difficult debug.
2. Before each
recursive calls current values of the variables in the function is stored in
the PCB (process control block) and then pushed to the OS Stack. Therefore, it
occupies lot of free memory.
3. Execution also
need more time than a simple iterative program.
Tail Recursion :
Tail recursive function where
all recursive calls are tail calls,
making a tail-recursive call, the caller's return position need not be
saved on the call stack. When the recursive call returns, it will branch
directly on the previously saved return position. Therefore, tail recursion saves both space and time.
Here is some Examples of
Recursive function.
Print the sum of
first ten natural number.
#include <stdio.h>
#include <conio.h>
//Sum of first ten natural no.
int Sum(int n, int s)
{
if(n<=10)
{
s=s+n;
printf("%d
",n);
return Sum(n+1,s);
}
else
return
s;
}
void main()
{
int
s=0;
clrscr();
s=Sum(1,s);
printf(" Sum = %d",s);
getch();
}
Factorial of a given no.
#include <stdio.h>
#include <conio.h>
//Factorial
int
fact(int n) //Recursive function
{
if(n<=1)
{
return
n; // return to main program
}
else
{
return
n*fact(n-1); // calling self
}
}
void main()
{
int n,f;
clrscr();
printf("Enter
a No. : ");
scanf("%d",&n);
f=fact(n);
printf("Factorial
of %d =
%d ",n,f);
getch();
}
GCD of two numbers. [This one
is an example of Tail Recursion]
#include <conio.h>
#include <stdlib.h>
//GCD
int gcd(int x,int y)
{
if(y==0)
{
return
x;
}
else
{
return
gcd (y,x%y);
}
}
void main()
{
int a,b,c;
clrscr();
printf("\n\tEmter First No. : " );
scanf("%d",&a);
printf("\n\tEmter Second No. : " );
scanf("%d",&b);
c=gcd(max(a,b),min(a,b));
printf("\n\tGCD of %d and %d = %d
",a,b,c);
getch();
}
Print first 10 Fibonacci
Seriers.
#include <stdio.h>
#include <conio.h>
//Fibonacci Series
void fibo(int a, int b, int i)
{
if(i<10)
{
printf("\n
%d ",a+b);
fibo(b,a+b,i+1);
}
}
void main()
{
clrscr();
printf("\n
0");
fibo(1,0,1);
getch();
}
Reverse Fibonacci Series (using
iteration method you will find it very though).
#include <stdio.h>
#include <conio.h>
//Reverse Fibonacci Series
void fibo(int n,int a,int b,int c)
{
if(n>1)
{
c=a+b;
fibo(n-1,b,c,c);
}
if(n>1)
printf("
%d ",c);
}
// main function
void main()
{
int n;
clrscr();
printf("Enter a No. : ");
scanf("%d",&n);
fibo(n,1,0,0); // 1 and 0 the value of a
and b
printf("0 ");
getch();
}
Check a given numbers is prime
or not
#include <stdio.h>
#include <conio.h>
//Prime No.
int
IsPrime(int n,int i)
{
if(i<n)
{
if(n%i==0)
{
return
1;
}
else
{
return
IsPrime(n,i+1);
}
}
return
0;
}
void
main()
{
int
n;
clrscr();
printf("\nEnter
a No. : ");
scanf("%d",&n);
if(IsPrime(n,2)==0)
{
printf("\n\t%d
is a prime ",n);
}
else
{
printf("\n\t%d
is not a prime ",n);
}
getch();
}
Print Armstrong numbers between
1 to 1000
#include <stdio.h>
#include <conio.h>
// Recursion for nested Loop to print Armstrong numbers
int
Armstrong(int n,int s)
{
int r;
if(n>0)
{
r=n%10;
s=s+r*r*r;
Armstrong(n/10,s);
}
else
// printf("\n%d
",s);
return
s;
}
void
Arm(int Limit)
{ int s;
if(Limit>=1)
{
s=Armstrong(Limit,0);
Arm(Limit-1);
}
if(s==Limit)
printf("\n%d
",s);
}
void
main()
{
int
n;
clrscr();
printf("\nEnter
the Limits : ");
scanf("%d",&n);
Arm(n);
getch();
}
Print the following Pattern using Recursion.
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
#include <stdio.h>
#include <conio.h>
// Recursion for nested Loop
void
Pattern(int i,int j)
{
if(j<=(i*2)-1)
{
printf("*");
Pattern(i,j+1);
}
}
void
Pat(int i,int size)
{
if(i<=size)
{
Pattern(i,1);
printf("\n");
Pat(i+1,size);
}
}
void
main()
{
int
n;
clrscr();
printf("\nEnter
the Limits : ");
scanf("%d",&n);
Pat(1,n);
getch();
}
No comments:
Post a Comment