+ 1
Please explain the output of the program.(how the algorithm is working?)
public class Program { static void check(int y){ if(y>=0){ System.out.print(y+" "); check(y-1); System.out.print(y+" "); } } public static void main(String[] args) { check(3); } }
1 Answer
+ 2
The output of this code will be :
32100123
Lets take a look at function check. It takes as parameter an integer y. If y is greater than 0, then print y and check for (y - 1). Then, it returns and print again y.
Example with 2
2 >= 0
print 2
(2 - 1) >= 0
print 1
(1 - 1) >= 0
print 0
(0 - 1) < 0
print 0
print 1
print 2