Recursive method [Java]

Recursion

What is the recursive method?

That itself is a recursive methodCalls itself

Recursion should not be too much
recursion must be exported (end condition)

The demand numbers between 5-1 and:

The method defined summation (recursively):

public static int getSum(int num) {
	if(num == 1) {
		return 1;
	}
	return num + getSum(num-1);
}

And an output method for receiving:

int sum = getSum(5);
syso(sum);

Recursive summation ideas:
seeking number of requirements that is between 5-1 and
5 + 3 + 2 + 4 + 1
5 + (and between 4-1)
4 + (and between 3-1)
3 + (and between 2-1)
2 + 1
recursively (n) - and between the (n-1)

Published 38 original articles · won praise 4 · Views 829

Guess you like

Origin blog.csdn.net/Hide111/article/details/104975540