logo CodeStepByStep logo

You are working on problem set: Unit 4 ( Pause)

mirrorSequence

Language/Type: Java recursion

Write a recursive method named mirrorSequence that accepts an integer parameter n and prints a sequence of n numbers as in the table below. For odd numbers the sequence has a single 1 in the middle, while for even values it has two 1s in the middle. Throw an IllegalArgumentException if passed a value less than 1.

Call Output
mirrorSequence(1); 1
mirrorSequence(2); 1 1
mirrorSequence(3); 2 1 2
mirrorSequence(4); 2 1 1 2
mirrorSequence(5); 3 2 1 2 3
mirrorSequence(6); 3 2 1 1 2 3
mirrorSequence(7); 4 3 2 1 2 3 4
mirrorSequence(8); 4 3 2 1 1 2 3 4
mirrorSequence(9); 5 4 3 2 1 2 3 4 5
mirrorSequence(10); 5 4 3 2 1 1 2 3 4 5
Method: Write a Java method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? .

If something seems wrong with our site, please

Is there a problem? Contact us.