
Patrick B. answered 03/17/21
Math and computer tutor/teacher
using namespace std;
#include <iostream>
#include <vector>
long factorial ( int n)
{
long longIntNumFactorialReturn =-1;
if (n>1)
{
longIntNumFactorialReturn = n * factorial(n-1);
}
else if (n<0)
{
cout << "Factorial of negative number undefined" << endl;
}
else //n==0 or n==1
{
longIntNumFactorialReturn=1;
}
return(longIntNumFactorialReturn);
}
int main()
{
int N=-1;
vector<int> intNumVec;
vector<long> factorialVec;
int iNum;
long factorialResult;
while (N<0)
{
cout << "How many ??? :>";
cin >> N;
}
for (int iLoop=0; iLoop<N; iLoop++)
{
cout << "Please input the integer # " << (iLoop+1) << ":>";
cin >> iNum;
intNumVec.push_back(iNum);
factorialResult = factorial(iNum);
factorialVec.push_back(factorialResult);
}
for (int iLoop=0; iLoop<N; iLoop++)
{
cout << intNumVec[iLoop] << "! = " << factorialVec[iLoop] << endl;
}
}