What is the output of this recursive C++ function?

#include <iostream>
using namespace std;
int fun(int n)
{
    if (n == 1)
        return 1;
    return n * fun(n - 1);
}
int main()
{
    cout << fun(5);
    return 0;
}
                        

Options

  • A. 15
  • B. 25
  • C. 120
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

C. 120

Detailed Explanation

Correct Answer: Option C

Explanation:
The function computes 5 * fun(4), then 4 * fun(3), and continues until fun(1) returns 1. The result is 5 * 4 * 3 * 2 * 1 = 120.