what will be the output of this program?
public static void print(int n) {
if (n > 0) {
System.out.println("hello");
print(n - 1);
}
System.out.println("world");
}
Read full article from Output of the recursive program - PuzzlersWorld.compublic static void print(int n) {
if (n > 0) {
System.out.println("hello");
print(n - 1);
}
System.out.println("world");
}
It will print, N times “hello” and then by N+1 times “world”.
First it will call print function recursively N times and will print world after it comes out of the if block. “world” is printed N+1 times since when n ==0, it will not go inside if block and will print world and return.
First it will call print function recursively N times and will print world after it comes out of the if block. “world” is printed N+1 times since when n ==0, it will not go inside if block and will print world and return.
No comments:
Post a Comment