Consider the following recursive method public static int ru
Consider the following recursive method:
public static int run1(int n){
if(n < 1)
return 3;
else
return 3 + run1(n-1);
}
What is run1(4)? How many times does the method call itself?
Solution
Answer: 3 times the method call itself and it will product the output 15
int this function, we are calling this function with value n is 4
this recursve calls will stop when n value is <1. and everytime we are decresing n value by 1.
So when first recursive call happens n value n-1 = 4- 1 = 3
So when second recursive call happens n value n-1 = 3- 1 = 2
So when third recursive call happens n value n-1 = 2- 1 = 1
So when fourth recursive call happens n value n-1 = 1- 1 = 0
Value will return like this 3 + 3 + 3 + 3 + 3 = 15
