recursive function

Recursion

A recursive function is a function that calls itself either directly or indirectly through another function.

Recursive function calling is often the simplest method to encode specific types of situations where the operation to be encoded can be eventually simplified into a series of more basic operations of the same type as the original complex operation.

This is especially true of certain types of mathematical functions. For example to evaluate the factorial of a number, n

n! = n * n-1 * n-2 * …  * 3 * 2 * 1.

We can simplify this operation into

n! =  n * (n-1)!

 

Program to evaluate the factorial of a number using recursion.

#include <stdio.h>

short factorial( short ) ;

void main()

{

short i ;

printf(“Enter an integer and i will try to calculate its factorial : “ ) ;

scanf( “%d”, &i ) ;

printf( “\n\nThe factorial of %d, %d! is %d\n”, i, i, factorial( i ) ) ;

}

short factorial( short num )

{

if ( num <= 1 )

return 1 ;

else

return ( num * factorial( num – 1 ) ) ;

}

error: Content is protected !!