Sunday, April 21, 2019

Recursion

When The Function Is Called Within The Same Function, It Is Known As Recursion. The Function Which Calls The Same Function Is Known As Recursion.

A Function That Calls Itself And Doesn’t Perform Any Task After Calling Function,It Is Called As TailRecursion.

In TailRecursion We Generally Call The Same Function With return statement

 

Syntax

returntype recursionfuction()

{

recursive fuction();

}

 

Example

Factorial Of A Number

#include<stdio.h>

#include<conio.h>

long int multiply numbers(int n)

{

if (n<0)

{

printf(“wrong value\n”);

getch;

exit(0);

}

if(n==0)

return 1;

return n*multiply numbers(n-1);

}

 

void main()

{

int n:

clrscr();

printf(“enter any positive interger”);

scanf(“%d”,&n);

printf(“factorial of %d is %d”,n,multiplynumvers(n));

getch();

}

 

Result 

Factorial Of 5 Is 120

 

Leave a Reply