Recursion in the ordinary development process is not used too much, but as a classical algorithm, we must also understand that recursion in the process of use must have a recursive end conditions, otherwise there will be a Java memory overflow, resulting in the entire system crashes. The following recursive algorithm is used to calculate the cumulative sum of the number within 100 to give us a deeper understanding of recursive algorithms.
public class Testdemo {
/**
* @param args
*
/public static void main (string[] args) {
SYSTEM.OUT.PRINTLN (sum);
/**
* @param sum method
*
/public static int sum (int num) {
if (num = 1) {return
1;
}
return num + sum (num-1);
}
The above code is a simple implementation of the Class A recursive call, when NUM is not equal to 1, the method has been called on its own, until the num==1, return 1, recursive call end.
Note: When using recursion, the recursive method must have an end condition.