Need help with recursive problem

Hi, could someone please explain to me how the following code results in 24, i understood it in class but totally forgot now and i have a mid term tomorrow. Please help. Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  #include <iostream>
using std::cout;
using std::endl;

int rose(int n);
//precondition: n >= 0

int main()
{
	cout << rose(4) << endl;
	return 0;
}
int rose(int n)
{
	if (n <= 0)
		return 1;
	else
		return (rose(n - 1)*n);
}
Last edited on
rose(4) = rose(3)*4 = rose(2)*3*4 = rose(1)*2*3*4 = rose(0)*1*2*3*4 = 24
Topic archived. No new replies allowed.