C Programming Tutorial
Computer Tutorial

Recursion



Recursion

In C, a function can call itself. In this case, the function is said to be recursive. Recursion is the process of defining something in terms of itself, and is sometimes called circular definition.
The non-recursive function for calculating the factorial value of an integer.

A Program to demonstrate recursion

\* C Program to demonstrate recursion *\
# include < stdio.h >
int   main( )
{

int   a, fact  ;
printf("\n Enter any number :") ;
scanf(" %d ",  &a ) ;
fact = rec ( a ) ;
printf(" Factorial value = %d ",  fact ) ;
return ( 0 ) ;

}
rec (  int x
{

int   f  ;
if ( x == 1 )
return ( 1 ) ;
else
f = x * rec ( x - 1 ) ;
return ( f ) ;

}

Output :

Enter any number : 5
Factorial value = 120