C Programming language

adplus-dvertising
Recursive Function in C
Previous Home Next

Recursion in C

Recursion is the property of function, in which function calls itself. In C, it is possible for the functions to call themselves. A function is called 'recursive' if a statement within the body of a function calls the same function.

Let us now see an example of recursion. Suppose we wan to calculate the factorial of an integer. As we know the factorial of a number is the product of all the integers between 1 and that number.

For Example : 4 factorial = 4*3*2*1. This also be expressed as 4!= 4*3!. Thus factorial of a number can be expressed in form of itself.

#include <stdio.h>
#include <conio.h>
int factorial (int);
void main
{
int a, fact;
clrscr();

printf("\n Enter any number");
scanf ("%d",&a);
fact=fatorial(a);
printf("factorial value=%d",fact);
}
int factorial(int x)
{
int f=1,i;
for(i=x; i>=1; i--)
f=f*i;
return(f);
}
 

Output :

Enter any number=3

Factorial Value=6

Previous Home Next