• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Recursion in C

Recursion in C with Examples and its Advantages

Function calling itself is called Recurssion

 

Advantages :

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

 

Drawbacks:

  • It is a very slow process due to stack overlapping
  • Recursive related f’ns will create infinite loops.
  • Recursive related programs are get 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

 

29

30

 

 

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);

}

 

31

 

32

 

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 are classified into two types. i.e internal recursive process, external recursive process
  • Whenever a f’n is calling itself, then it is called internal recursive process.
  • Whenever a recursive f’n is calling another recursive f’n, then it is called 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

 

33

 

 

void main()

{

int a=2;

++a;

printf(“%d”,a);

if(a<=3);

main()

printf(“%d”,a);

}

o/p : 3        3       3       …………………………. stack over flow

  • it is possible to call 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);
}

34

 

 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

 

 

 

Summary
Review Date
Reviewed Item
Recursion in C
Author Rating
5

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Recursion in C"

Leave a Message

Your email address will not be published. Required fields are marked *

Site Disclaimer, Copyright © 2016 - All Rights Reserved.