# Factorial Computation Program

• 05-11-2008
ts9818a
Factorial Computation Program
Ok, so Im trying to write a program that computes factorials recursively.

Here is what Ive got (when I asked my teacher for help, he said my factorial.cpp has one key flaw but I cant find it)

Factorial.cpp

Code:

```int factorial(int N) {   //cout << "into factorial with " << N << endl;   if ( N < 1 )       return 1; // base case   //cout << " not a base case with N = " << N << endl;   int answer = factorial(N-1);   //cout << " returning " << answer << " as answer at N = " << N << endl;   return answer; }```

driveFactorial.cpp
Code:

``` #include <iostream> #include <fstream> using namespace std;     #include "factorial.cpp"     int main() {   int FactForN = 1;     cout << "\n Factorial of what number?  (negative to end) ";   while ( cin >> FactForN && FactForN > 0)   {     int answer = factorial(FactForN);     cout << "\n The factorial of " << FactForN << " is " << answer << endl;     cout << "\n Factorial of what number?  (negative to end) ";   } }```

any help would be appreciated. Thanks!
• 05-11-2008
DaveH
Code:

`if ( N < 1 )`
There is no 0 in factorials. Your base case would be when N = 1.
• 05-11-2008
ts9818a
so I should make it n=1 and not n <1?
• 05-11-2008
NeonBlack
0! is 1 so his base case is correct.

If we use 0!=1 as our base case then in general,
N! = N*(N-1)!
Compare that to what you are doing.

ALSO, you should include .h files, not .c files