#include // space over some to show how deep we are in recursion void space (int num){ const int max_space = 10; // space more when number is lower - thats when we are deeper int times = max_space - num; // go over three spaces for each for (int cnt = 0; cnt < times; cnt++) { cout << " "; } } // recursively calculate factorial int Factorial(int n){ cout << endl; space(n); cout << "in factorial with n = " << n << endl; // termination part - time to stop recursion // 0! is defined to be 1 if (n == 0){ cout << endl; space(n); cout << "ending factorial with n = " << n << " and result = 1" << endl; return 1; } // bigger number - n! is n * (n - 1)! // (recurse on smaller number) else { int result = n * Factorial(n - 1); cout << endl; space(n); cout << "ending factorial with n = " << n << " and result = " << result << endl; return result; } } // try recursive factorial function out for 5 int main() { cout << "The result for 5 factorial" << endl; int res = Factorial(5); cout << " is: " << res << endl; return 0; }