### Recursive Function

I'm practicing writing my own functions, recursive functions to be exact. My goal is to have it count up to a number, and then back down (a number the user inputs). It's required I have the function inside as well.

This is what I have so far. I expect it to display as...
input:4

1
2
3
4
4
3
2
1

input:4

4
3
2
1
1
2
3
4

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849`` `````` #include using namespace std; int display(double number); int main() { double number, calls; do{ cout << "Enter number "; cin >> number; cout << endl; calls = display(number); cout << endl; } while (1); system("pause"); return 0; } int display (double number) { int calls; cout << number << endl; if(number == 1) calls = 1; else calls = display(number-1); cout << number << endl; return calls; } ``````
Last edited on
what your program does now is that it starts at 4(since that is the first input), and it keeps subtracting 1 until it has reached one.

if you want your desired output, then you should keep adding one, until it's 4.
you could do it this way
 ``12345678910111213`` ``````int display (double max, double start) { int calls; cout<

I haven't tested the code myself, but I think it might work.
start is the number with what it should begin (in your case: 1).
That works. Thank you!
Topic archived. No new replies allowed.