R Recursion (1)
R Recursion (1)
Steps for implementing Step 3- If the base case condition is not met, it will break
recursion in a function down the large complex problem into subproblems to
are as follows: reduce its size.
FACTORIAL(FACT, N)
without if (N == 0)
Recursion {
return 1;
}
for (int K = 1; K <= N; K++)
{
FACT = FACT*K;
}
return FACT;
}
FACTORIAL(FACT, N)
This procedure calculates N! and
returns the value in the variable FACT.
Printing }
Numbers
int fun(int x)
{
if(x<=0)
return 1;
else
fun(x-1);
cout<<x;
}
int main()
{
int g(int);
cout<<g(5);
}
int g(int n)
With {
Recursion int f;
if(n==1)
return 1;
else
f=n+g(n-1);
return(f);
int main()
{
int fact(int n);
int f=fact(5);
cout<<f;
}
Factorial int fact(int n)
{
if(n==0||n==1)
return 1;
else
return(n*fact(n-1));
}
FIBONACCI(FIB, N)
This procedure calculates the NN-th Fibonacci number
and returns the value in the variable FIB.
1.If NN = 0:
Set FIB := 0, and Return.
2.If NN = 1:
Fibonacci Set FIB := 1, and Return.