A function that calls itself is called as a recursive function and the implementation of a program that uses recursive function is called as recursion.
A recursive function must definitely have a condition that exits from calling the function again. Hence there must be a condition that calls the function itself if that condition is true.
If the condition is false then it will exit from the loop of calling itself again.
The condition could also be implemented vice verse i.e. if the condition is false then the function calls itself else if the condition is true, it will exit from the loop calling itself again
Program ::
Algorithm :
main() function
Step I : START
Step II : PRINT “Enter a number”.
Step III : INPUT a
Step IV : fact = CALL factorial (arguments: a).
Step V : PRINT fact
Step VI : STOP.
factorial (parameters: a)
Step I : START.
Step II : IF a = 1, THEN RETURN (1)
ELSE RETURN ( a * CALL factorial(arguments: a – 1))
#include<stdio.h>
#include<conio.h>
void main()
{
int no,factorial;
int fact (int no);
clrscr();
printf("Enter a number:");
scanf("%d",&no);
factorial=fact(no);
printf("Factorial=%d",factorial);
getch();
}
int fact (int no)
{
if(no==1)
return 1;
else
return (no * fact (no-1));
}