Explore
ProGrad Programs
Placement Prep
About Us

Edit
Reply

Edit

# Recursion in C

Published on 06 Mar 2020

Recursive Functions: A function which calls itself repeatedly until a specified condition is satisfied, is called a recursive function. The C programming language supports recursion functions, but when using recursion, programmers need to be careful to define an exit condition from the function, otherwise, it will go into an infinite loop. Recursion functions are very useful in calculating factorial of a number and generating fibonacci series etc.

Example: Recursive function to find the factorial of 'n'.

#include
int factorial(int n)
{
if(n<= 1 )
return 1;
else
{
retum(n*factorial(n-1));
}
}

int main()
{
int n;
printf("Enter a number:");
scanf("%d",&n);
printf("Factorial is %d\n",factorial(n));
getch();
return 0;
}


Assume n=5, then factorial(5) is called from the main()

(n<=1) is false

In the else part, n*factorial(n-1) is evaluated.

If you have any feedback about this article and want to improve this, please write to enquiry@faceprep.in
Explore 'c'