The thread has not been reviewed by Editors yet. Readers are advised to use their best judgement before accessing this thread.

This thread will be reviewed shortly.

If you think this thread contain inappropriate content, please report to

webmaster.

This program require recursion for finding factorial of given number. Before going to recursive function we first need to understand the concept of function.

Function is a program which performs a predefined task.

syntax of function declaration is

return data type name of function(argument list separated by comma);

Function has three main parts namely

1) declaration of function.

e.g. int sum(int,int); This statement tells the compiler that sum is the function which requires two integers as arguments and it returns an integer.

2) Call to function. e.g. sum(a,b); where a and b are predefined and initialized integers.

3) Definition of function. e,g int sum(int x, int y) { body of function;}

Note that in declaration and in call to function there is a use of ; as termination point but in definition of function no such ; is used because there exist a code of that function inside the pairs of curly braces.

Recursive function means if function is calling itself then function is said to be recursive in nature. The prerequisite of recursive function is that it must have at least one if statement to stop the recursive call. If no such if statement exist then recursive function never stop its execution means it become endless.

Following is the program to find factorial of given number using recursive function.

Please include stdio.h and conio.h header files before void main(void) statement.

//PROGRAM TO IMPLEMENT FACTORIAL OF GIVEN NUMBER USING RECURSION.

#include

#include

void main(void)

{

long int n,r;

int factorial(int);

clrscr();

printf("Enter the number to find factorial\n");

scanf("%ld",&n);

r=factorial(n);

printf("\nThe number is %ld and its factorial is %ld",n,r);

getch();

}

int factorial(int num)

{

long int f;

if(num==1)

{

return(1);

}

else

{

f=num*factorial(num-1);

}

return(f);

}

In above program factorial is the function having one integer as an arguments and it returns one integer as a result. In main function when statement r=factorial(n); occurs then control come out of the scope of main function and definition of factorial function gets executed. During this process value of variable n get copied into variable num( because this is call by value type function call).

In side factorial function firstly value of num is checked with value 1 and if it is not true then it will call factorial function recursively with new value which is num-1.

So the statements as below get created if we consider value of num is 5.

f=5*factorial(4);

f=4*factorial(3);

f=3*factorial(2);

f=2*factorial(1);

When call of factorial(1) get executed then it returns value 1 so statement f=2*factorial(1) becomes f=2*1 i.e. 2. This value get return to statement f=3*facttorial(2) so it becomes f=3*2 i.e. 6 and so on.

In recursive functions recursive statements are hold on stack and get executed in reverse order. This is because stack operates in Last In First Out(LIFO) manner.

As per prerequisite of recursive function in the above program if statement is used to stop the recursive call. When value of num becomes 1, recursive call stop.

In above program long int type variables are used so that user can easily find out factorial of any required number because size of long integer is large enough to hold the required multiplication.