What is the output of the following C++ program?
#include <iostream>
using namespace std;
long factorial(long n)
{
if (n <= 1)
return 1;
return n * factorial(n - 1);
}
int main()
{
cout << factorial(4);
return 0;
}
Options
-
A.
10
-
B.
16
-
C.
24
-
D.
More than one of the above
-
E.
None of the above
Correct Answer (Detailed Explanation is Below)
C.
24
Detailed Explanation
Correct Answer: Option CExplanation:The function calculates
4 * 3 * 2 * 1 = 24. Each recursive call reduces n by 1 until the base case
n <= 1 is reached.