Recusrion function:
1. Follow the stack
2. Height of stack is almost equal to height of n // in the given code
The cycle to call function.
1.Main to call function
2.Base
3.Work
Like ex.
public class Recursion{
public static int cal(int x, int n)
{
// Code execute when filling of stack
// First base 1
If(n==0){
return 1;
}
//Second base 2
If(x==0){
return 0;
}
// Work
//calling function recursion
int xpm= cal(x, n-1);
// The code is executing below this when in deleting the level of stack
int xpn= x* xpm;
return xpn
}
//Main to call the function first time
public static void main(String args[]){
int x=2, n= 5;
System.out.println(cal(x,5));
}
}
Comments
Post a Comment