/* Implements a factorial function fact recursively. If the user inputs a negative number by mistake, it returns the error value 0. This avoids the possibility of infinite recursion. It will still give the wrong value if n>12 because the value will go beyond the maximum range of long unsigned int. */ #include long unsigned int fact(int); void main() { int num; cout << "Enter a nonnegative integer: "; cin >> num; cout << num << "! is " << fact(num) << ".\n"; } long unsigned int fact(int n) { if (n < 0) return 0; if (n == 0) return 1; return n*fact(n-1); }