I need some help with recursive methods.
Here's a method.
public int m1(int n)
{
If(n==1)
return 25;
else
return n + m1(n-1)
}
then you solve for:
m1(6)
alright, so I get you this up to the 25. How does the 25 get there, why wouldn't it be 1+m1(0)? and the answer for the problem is 45. Could someone explain how you get 25 and 45?
6+m1(5)
5+m1(4)
4+m1(3)
3+m1(2)
2+ m1(1)
25
m1(6)==45
thanks.
> How does the 25 get there, why wouldn't it be 1+m1(0)?Because the method is defined that way. Recursion stops when argument value is 1. An in that case, it returns 25.Is there something you don't understand in the if/else statement?
> How does the 25 get there
You put it there.
> the answer for the problem is 45
It may be easier to see this if you write your list in the reverse order:m(1) = 25
m(2) = 2 + m(1)= 27
m(3) = 3 + m(2)= 30
m(4) = 4 + m(3)= 34
m(5) = 5 + m(4)= 39
m(6) = 6 + m(5)= 45