• # Program to find factorial of given number using recursion.

Preparing for Diploma exams? Looking out for using help to solve a problem of writing a factorial program using recursion? Check out this aSk Expert page where you can find the program solution to find factorial of a given number using recursion.

Question paper of Diploma in Computer Engineering (affiliated by MSBTE, Mumbai) for Summer 2018 contain the following question. This question carry 4 marks. It is expected that some explanation about the question is given. This question is asked in question paper of PIC(Programming in C) of I scheme with subject code 22226. So waiting for a solution to the asked question in C language only.
Q) Develop a program to find factorial of a number using recursion. [ 4 Marks]

• 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.
//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.