Recursion in C with Examples and its Advantages

Recursion in C Advantages 

  • By using the recursion process only function calling information will maintain by the compiler.
  • stack evaluation will take place by using recursion only
  • in fiz, prefer and postfix notifications will take place by using response only.

Recursion in C Drawbacks:

  • It is a very slow process due to stack overlapping
  • Recursive related f’ns will create infinite loops.
  • Recursive related programs are getting a stack overflow.

void abc()

{

auto int a;

static int <=3;

a=++s;

printf(“n %d %d”, a,s);

if(a<=6)

abc();

printf(“n %d %d”,a,s);

}

void main()

{

abc();

abc();

}

  o/p : 4        4 5       5 6       6 7       7 6       7 5       7 4       7 8       8 8       8  

Recursion in C

Recursion in C    

void xyz()

{

int a;

static int s;

a=s++;

printf(“n %d %d”,a,s);

if(a<=3)

xyz();

printf(“n %d %d”,a,s);

}

Recursion in C

  Recursion in C

int g=5;

void main()

{

auto int a;

static int s=5;

a=s--;

++g;

printf(“n %d %d %d”,a,s,g);

if(a>=3)

abc();

xyz();

}

void xzyz()

{

int a;

static int s;

a=++s;

++g;

printf(“|n %d %d %d”,a,s,g);

if(a<=3)

{

xyz();

printf(“n %d %d %d”,a,s,g);

}

5                    4              6                           1       1       84                    3              7                           2       2       7 3                    2              8                           3       3       6 2                    1              9                           4       4       5 2                    1              9                           4       4       5 3                    1              9                           3       4       5 4                    1              9                           2       4       5 1                    1              9                           1       4       5

   

  • Recursion in C is classified into two types. i.e internal recursive process, external recursive process
  • Whenever a f’n is calling itself, then it is called an internal recursive process.
  • Whenever a recursive f’n is calling another recursive f’n, then it is called an external recursive process.
  • extern int g;

void abc()

{

int a;

static int s=5;

a=--s;

--g;

printf(“n %d %d”,a,s,g);

if(a>=3)

{

abc();

printf(“n %d %d %d”,a,s,g);

}

void main()

{

void xyz(void); // declaration

xyz();

}

void xyz()

{

auto int a;

static int s;

a=s++;

++g;

printf(“n %d %d %d”,a,s,g);

if(a<2)

{

abc();

xyz();

}

printf(“n %d %d %d”,a,s,g);

}

int g=5;

 

O/P:-

          0       1      6 - - > xyz1         2       3       5- -> xyz34      4       5- - >abc           2       3       5- -> xyz33      3       4-- ->abc3         1       3       5- - >xyz2 2      2       3- - >abc3 2      2       3- - >abc2 4      2       3- - >abc 1 1     2       4- - >xyz2 1     1       3- - > 2nd call abc 1     1       3- - > 2nd call abc

  Recursion in C

void main()

{

int a=2;

++a;

printf(“%d”,a);

if(a<=3);

main()

printf(“%d”,a);

}

o/p : 3        3       3       …………………………. stack overflow

  • it is possible to call the main function itself also
  • When we are making the recursion of the main f’n, by using auto variable. then we will get stack overflow because the life time of the auto variable is restricted, with in the body only

Void main()

{

static int s=1;

++s;

printf(“%d”,s);

}

Recursion in C  

 o/p :  2      3       4       4       4  

int power(intb, int e)

{

if(e<0)

return 0;

elseif(e==0)

return 1;

else

return (b*power(b,e-1));

}

void main()

{

int a,b,p;

clrscr();

printf(“n enter value of a:”);

scanf(“%d”,&a);

printf(“n enter value of b:”);

scanf(“%d”, &b);

p=power(a,b);

printf(“n %d ^ %d value is =%d”,a,b,p);

getch();

}

o/p : 25 = 32  

in fact(int n)

{

if(n<0);

return 0;

else if (n<=1)

return 1;

else

return(n*fact(n-1));

}

void main()

{

int n,f

clrscr();

printf(“Enter a value :”);

scanf(“%d”, &n);

f=fact(n);

printf(“n %d fact value is : %d”,n,f);

getch();

o/p : Enter a value 5      

For an in-depth understanding of Pointers in C click on