// factorials.cc
// Recursive and interative algorithms for computing factorials (N!).

// Standard C++ header files
#include <iostream>

// Forward definitions of auxiliary functions
long recFactorial(long n);
long iterFactorial(long n);

int main()
{
  long number;
  while(true) {
    cout << "Please enter a positive number (or negative to end): ";
    cin >> number;
    if (number < 0) return 0;
    cout << "Recursive: " << number << "! = " << recFactorial(number) << endl;
    cout << "Iterative: " << number << "! = " << iterFactorial(number) << endl;
  }
}

long recFactorial(long n)
{
  if (n==0) {
    return 1;
  }
  else {
    return (n * recFactorial(n - 1));
  }
}


long iterFactorial(long n)
{
  long product = 1;
  for (long i=1; i<=n; i++) {
    product *= i;
  }
  return product;
}
