Loading...
Loading...
Loading...
A rocket show counts down to 0 and then back up: for n = 3 it prints 3 2 1 0 1 2 3.
Write a recursive method static void wave(int n) that prints this wave. Do not use any loop anywhere in the program.
Example 1
Input
3
Output
3 2 1 0 1 2 3
Down to 0, then back up.
Example 2
Input
0
Output
0
Only the 0 is printed.
Try the problem first. Open one hint at a time.
Base case: for n = 0 print just 0.
Otherwise print n and a space, let the recursive call wave(n - 1) print the middle part, then print a space and n again.
Code written after the recursive call runs while the calls return, which is what makes the count go back up.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor