// This program demonstrates a recursive function to // calculate the factorial of a number. #include using namespace std; // Function prototype int factorial(int); int main() { int number; // Get a number from the user. cout << "Enter an integer value and I will display\n"; cout << "its factorial: "; cin >> number; // Display the factorial of the number. cout << "The factorial of " << number << " is "; cout << factorial(number) << endl; return 0; } //************************************************************* // Definition of factorial. A recursive function to calculate * // the factorial of the parameter n. * //************************************************************* int factorial(int n) { cout << "Starting f(" << n << ")\n"; if (n == 0) { cout << "Base case, returning 1\n"; return 1; // Base case } else { cout << "Calling f(" << n-1 << ")\n"; int temp = n * factorial(n - 1); // Recursive case cout << "returning " << temp << " from f( " << n << ")" << endl; return temp; } }