C Program to Find Factorial of a Number Using Recursion

Lastly I write the Program to find factorial of a number simply. Now I will tell you the same, using Recursion.

As you know the Recursion is a technique in which function call itself.

Let’s See The Program ►►

#include<stdio.h>
#include<conio.h>
int fact(int)   //Function Prototype Declaration

void main()
{
int num;
printf("Enter Number to find it's factorial");
scanf("%d",&num);

//Function call inside the printf
printf("Factorial = %d\n",fact(num));

getch();
}

int fact(int num)   //Function Definition
{
if(num==0)
return 1;

else
return (num*fact(num-1));
}

Explanation : What I did is made a function which will receive the number in the variable num. And when num is greater then zero it will follow the expression (num*fact(num-1));. For example we are finding the factorial of 5 then the expression will evaluate to (5*fact(4)); And then the function fact will be call from this statement with the argument 4. Then this expression will evaluate to (4*fact(3)); again the same process and so on. When the number comes to zero the function will return the value 1 and it will multiply to the num as (1*1) for the last term, then it will return also the value 1, then the expression will be (2*1), then it will return the value 2, then the expression will be (3*2) and so on. By this process the factorial of the number will be calculate. Recursion is really a good feature of C, which provides many things to get done easily.