Find Factorial Of a Number Using Recursion C Program | Coding Bot

Like Us On Facebook

Follow Us On Twitter

Thursday, 18 July 2013

Find Factorial Of a Number Using Recursion C Program

Hello Guys,
In this post I'll explain what is Recursion and how it can be used to calculate the factorial of a number in C.

Suppose f1(), f2() and f3() are three functions. The function f1() can call f1(), in turn, f2() can call f3(). Likewise, nesting of the function calls can be to any level depending on the requirement. There is one more possibility with regard to functions, which needs to be mentioned. That is, a function f() calling itself. The phenomenon of a function calling itself is called Recursion. The function involved in the process is referred to as a recursive function.

When a recursive function is employed to solve any problem, care needs to be taken to see to it that the function does not call itself again at some point of time by which the solution of the problem has been arrived at. This is accomplished with the help of a terminating condition.

The program is quite simple. Function Fact() is defined with an argument of int type and is made to return a Value of int type. The purpose of this function is to find the factorial of an integer number passed as an argument to it and return the result. It works as follows:

If the value of n is 1, the function returns 1. Of n>1 then, as long as n>1, fact(0 is called recursively by passing one less than the previous number. Once argument to fact() becomes 1, the function call is replaced by 1. The value of the resultant expression constructed is assigned to the variable f and is then returned to the main(). 

Find Factorial Of a Number Using Recursion C Program:

/* Double-Click To Select Code */


int fact(int number);

int main(void)
 int number;
 int f;

 printf("\nEnter The Number: ");

 f = fact(number);
 printf("\nFactorial of %d =  %d",number,f);

 return 0;

int fact(int number)
 int f;

 f = number * fact(number-1); // Recursion
 return f;

Program Output:

Find Factorial Of a Number Using Recursion C Program

Please Comment If You Liked The Post.

Do you like this post? Please link back to this article by copying one of the codes below.

URL: HTML link code: Forum link code:

No comments:

Post a Comment