Recursion


I have issus with this code:
How can i make it to display this condition. It has to be with recursion: 1, 2, 3, 4, 3, 2, 1 


1
2
3
4
5
6
7
8
9
  void rec(int n) {
if(n==1) {
cout<<1;
return;
}
cout<<n<<" ";
rec(n-1);
cout<<n<<" ";
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

void rec( int n, int r = 1 )
{
   cout << r << " ";
   if ( r == n ) return;
   rec( n, r + 1 );
   cout << r << " ";
}

int main()
{
   rec( 4 );
}
Topic archived. No new replies allowed.