116k views
4 votes
Consider the accompanying definition of a recursive function. What is the output of the following statement? cout << puzzle(3, 7) << endl; int puzzle(int start, int end) { if (start > end) return start - end; else if (start == end) return start + end; else return end * puzzle(start + 1, end - 1); }

A. 10
B. 21
C. 42
D. 420

User Trancot
by
6.0k points

1 Answer

4 votes

Answer:

Option D is the correct answer for the above question snip.

Step-by-step explanation:

Here Puzzle is a recursive function which takes two argument start and end which execute like this.

  • Firstly execution starts from main function and values 3 and 7 pass as an argument for the variable start and end.
  • Then In the Puzzle function, else block is execute because the value of the "start" variable is not greater or equal to the value of the "end" variable.
  • Then again Puzzle function is called with the value of 4 and 6 for the start and end variable.
  • One's time again puzzle function is called for the value of 5 and 5 for the start and end variable.

It means that puzzle function is called at three times--

  1. First time from main.
  2. And two times from puzzle function and return the value of 7*6(42).
  • Then the start and end value are the same and return the value of start+end which is 5+5(10).
  • Then the total value returned is 10*42 which is 420.
  • Then the output will be 420.
User DarbyM
by
7.3k points