C Programming language

Day 6 Recursive Function in C Programming language
 Previous Home Next

# Recursion in C Programming language

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.

Syntax:

void recursion()
{
recursion(); /* function calls itself */
}

int main()
{
recursion();
}

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>

int factorial(unsigned int i)
{
if(i <= 1)
{
return 1;
}
return i * factorial(i - 1);
}
int  main()
{
int i = 4;
printf("Factorial of %d is %d\n", i, factorial(i));
return 0;
}
Output:

Factorial of 4 is 24

 Previous Home Next