// Author: Keith Shomper // Date: 11/4/03 // Purpose: To demonstrate a recursive program #include using namespace std; int factorial(int n); int main() { int n, f; // prompt the user for the value of n cout << "What number would you like to compute the factorial for? "; cin >> n; f = factorial(n); // Display the result if (f > 0) { cout << n << "! is " << f << endl; } return 0; } int factorial(int n) { if ( n < 0) { cerr << "Error: Factorial not defined for negative number " << n << endl; return 0; } else if (n == 0) { return 1; } else { return n * factorial(n-1); } }