Recursion: Count from x until n and backwards
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;
    }
}