Recursion: Count from x until n and backwards I wrote this recursive function to print a number sequence from x to n and n to x back.

Here is one of the ideas I had to improve it, but it didn't work out (use another ternary operator at the end of the first one): 


return x &lt; n ? printNumberSequence(x + 1, n) : n = 0  x &gt; n  
       &amp;&amp; x != n ? printNumberSequence(x - 1, n) : n;



However, I do have a working solution below:

#include &lt;iostream&gt;
using namespace std;

int printNumberSequence(int x, int n)
{ //counts from x until n
    cout &lt;&lt; x &lt;&lt; endl;
    x &lt; n ? printNumberSequence(x + 1, n) : n = 0 ;
    return x &gt; n  &amp;&amp; x != n ? printNumberSequence(x - 1, n) : n;
}

int main()
{
    return printNumberSequence(0, 5);
}


I want to know if this way to solve the problem is good and how could I improve that code.
If you print a sequence of numbers from \$x\$ through \$n\$ in increasing order, and then the same sequence in decreasing order, you'll get a row of numbers which starts and ends with \$x\$ and contains an analogous double sequence of numbers \$x+1\ldots n\$ between, right?

For example a sequence from 2 to 5 and back is: 2, sequence from 3 to 5 and back, and 2 again:

2 [3 4 5 5 4 3] 2


So the simplest solution would be a recursion like this:

void printNumberSequence(int x, int n)
{
    cout &lt;&lt; x &lt;&lt; endl;
    if(x &lt; n) printNumberSequence(x + 1, n);
    cout &lt;&lt; x &lt;&lt; endl;
}


If you want to avoid doubling the greatest number, e.g. for numbers 2 through 5 you want the output like:

2 3 4 5 4 3 2


just execute the second cout &lt;&lt; ... under the if(), that is when you're not at the maximum number:

void printNumberSequence(int x, int n)
{
    cout &lt;&lt; x &lt;&lt; endl;
    if(x &lt; n)
    {
        printNumberSequence(x + 1, n);
        cout &lt;&lt; x &lt;&lt; endl;
    }
}