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!



LinkBack URL
About LinkBacks


