A method is calling itself is known as recursive method.Java programming language allows a method to call itself this is nothing but 'recursion'. Many Programming Problems solved by the recursion.The main advantage to recursive methods is that they can be used to create clearer and simpler versions of simpler algorithms that can their iterative relatives.For example, A Quick Sorting algorithm is very difficult to implement in iterative way. For Most of the algorithms implements in easy manner using recursion.
Recursive versions of many routines may execute a bit slow compare to iterative process. Since when a method calls itself, new local variables and parameters are allocated storage on the stack, and the method code is executed with these new variables from the start. As each recursive call returns, the old local variables and parameters are removed from the stack, and execution resumes at the point of the call inside the method.Because storage for parameters and local variables is on the stack and each new call creates a new copy of these variables, it is possible that the stack could be exhausted. If this occurs, the Java run-time system will cause an exception says StackOverFlowException.
Factorial program using for loop(iterative method):
class factorial
{
//recursive declaration of method factorial
public long factorial(long num)
{
long result=1;
//iterative declaration of method factorial
for(long i=num;i>=1;i--)
result=result*i;
return result;
}
public void displayfact()
{
//calculate the factorials 0 through 5
for(int counter=0;counter<=5;counter++)
System.out.printf("%d!=%d\n",counter,factorial(counter));
}
}
public class FactorialTest
{
public static void main(String args[])
{
factorial f=new factorial();
f.displayfact();
}
}
Output:
0!=1
1!=1
2!=4
3!=6
4!=24
5!=120
Let us understand this concept with factorial example using recursion:
class RecursionDemo
{
static long fact(int num)
{
long result;
if(num==1)
return 1;
result=fact(num-1)*num;
return result;
}
public static void main(String args[])
{
System.out.println("factorial of 5:");
System.out.println(RecursionDemo.fact(5));
}
}
Output:
factorial of 5:
120
Recursive versions of many routines may execute a bit slow compare to iterative process. Since when a method calls itself, new local variables and parameters are allocated storage on the stack, and the method code is executed with these new variables from the start. As each recursive call returns, the old local variables and parameters are removed from the stack, and execution resumes at the point of the call inside the method.Because storage for parameters and local variables is on the stack and each new call creates a new copy of these variables, it is possible that the stack could be exhausted. If this occurs, the Java run-time system will cause an exception says StackOverFlowException.
Factorial program using for loop(iterative method):
class factorial
{
//recursive declaration of method factorial
public long factorial(long num)
{
long result=1;
//iterative declaration of method factorial
for(long i=num;i>=1;i--)
result=result*i;
return result;
}
public void displayfact()
{
//calculate the factorials 0 through 5
for(int counter=0;counter<=5;counter++)
System.out.printf("%d!=%d\n",counter,factorial(counter));
}
}
public class FactorialTest
{
public static void main(String args[])
{
factorial f=new factorial();
f.displayfact();
}
}
Output:
0!=1
1!=1
2!=4
3!=6
4!=24
5!=120
Let us understand this concept with factorial example using recursion:
class RecursionDemo
{
static long fact(int num)
{
long result;
if(num==1)
return 1;
result=fact(num-1)*num;
return result;
}
public static void main(String args[])
{
System.out.println("factorial of 5:");
System.out.println(RecursionDemo.fact(5));
}
}
Output:
factorial of 5:
120
No comments:
Post a Comment