// // Program: factorial // // Purpose: test recursive function for computing n factorial. // // Input: // non-negative integer N from standard input. // Output: // N factorial printed to standard output. // #include #include // Pre: n >= 0. // Post: returns n-factorial. int factorial(const int n) { if (n == 0) return 1; else return n * factorial(n-1); } // Main program. int main(void) { int num; cout << "Enter a non-negative integer: \n"; cin >> num; if (num < 0) { cout << "Number must not be negative.\n"; return EXIT_FAILURE; } cout << "factorial(" << num << ") = " << factorial(num) << endl; return EXIT_SUCCESS; }