logo CodeStepByStep logo

NumberSequence

Language/Type: C# recursion
Related Links:

Write a recursive method named NumberSequence that accepts an integer n as a parameter and prints a sequence of n integers, descending from n to 1 and then ascending back from 1 to n as in the table below:

Call Output
NumberSequence(1); 1
NumberSequence(2); 1 1
NumberSequence(3); 2 1 2
NumberSequence(4); 2 1 1 2
NumberSequence(5); 3 2 1 2 3
NumberSequence(6); 3 2 1 1 2 3
NumberSequence(7); 4 3 2 1 2 3 4
NumberSequence(8); 4 3 2 1 1 2 3 4
NumberSequence(9); 5 4 3 2 1 2 3 4 5
NumberSequence(10); 5 4 3 2 1 1 2 3 4 5

Notice that for odd numbers the sequence has a single 1 in the middle while for even values it has two 1s in the middle.

Your method should throw an ArgumentException if passed a value less than 1.

Method: Write a C# 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? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.