+ 1
Please explain this recursive code output
Why does calling the recursive function inside println output return values from each iteration of the recursive function and not just the final return value 5? https://code.sololearn.com/c06eZE6t3ugg/?ref=app
1 Answer
+ 4
rec(2,5) = 2+(rec(3, 5))
=2+(3+rec(4, 5))
=2+(3+(4+"5"))
=2345
if you want to output only 5, change the code :
public class Program
{
public static String samplerecursive(int x, int y){
if(x==y)
return ""+y;
return samplerecursive(x+1,y);
}
public static void main(String[] args) {
System.out.println(samplerecursive(2,5));
}
}