What's wrong in the code for finding factorials?

#include
using namespace std;
int main(){
int T;
long n;
cin>>T;
for (int i=0; i<T; i++){
cin>>n;
long ans=1;
for (int i=1; i<=n; i++){
ans=ans*i;
}
cout<<ans<<endl;
}
return 0;
}

2 Likes

Consider the test input:

1
24
2 Likes

If your question is what is wrong with the code, then the answer is quite simple:
you have not mentioned which header file to include.

But if your question is besides that, then the code is correct but it can only find factorial of 0 to 20. You can use long long int to increase the range. And if the input is very high then you can use this approach, this approach uses arrays to store the value, hence giving factorials of large values.

1 Like