#include
using namespace std;
int main()
{
int i=1,t,n;
int f=1;
cin>>t;
while(t!=0)
{
cin>>n;
while(i<=n)
{
f=f*i;
i++;
}
cout<<f<<endl;
t–;
}
return 0;
}
https://www.codechef.com/submit/complete/19051902
This is an AC solution in C++. This uses a special library from Boost, as calculating very big values in C++ is very difficult as compared to Java which has a BigInteger class.
The logic is same, but do note the changes in your code.
You will have to reset the values of F and I every time you want to calculate a factorial of a different number.
What you did was correct if the values of the number was increasing or constant but never less than the previous number (somewhat like DP!).