# Can't understand what's wrong here

the code gives correct output in compiler,but on submission it gives wrong answer.
here’s my code

#include<bits/stdc++.h>
using namespace std;

int factorial(int ans){
if (ans == 1 || ans == 0)
return 1;
else{return ans*factorial(ans-1);}
}
int main (){
int T,x[10000];
cin>>T;
for(int i=0;i<T;i++){
cin>>x[i];
}
for(int i=0;i<T;i++){
cout<<factorial(x[i])<<endl;
}
return 0;
}

One thing that’s “wrong here” is that you haven’t actually told us which Problem you’re trying to solve

Assuming it’s FCTRL2, just search the forums for that - people make the same mistake over and over again. See e.g. FCTRL2 WRONG ANSWER - #7 by karan10xb .

1 Like

sorry,
it’s the problem for small factorials,
beginners level;

Okay, you can’t print the answer for values more than 30 like 100 or other larger values.
it will give 0 as output.

Learn about ranges of int, long int and other data types

even if it was for small factorials you should use long long int instead of int.

ok,thanks

hre’s my new code with long long int but this also doesn’t work
#include<bits/stdc++.h>

using namespace std;

int factorial(long long int ans){

if (ans == 1 || ans == 0)

``````return 1;
``````

else{return ans*factorial(ans-1);}

}

int main (){

long long int T;

``````long long int x[100];

if(1<=T<=100){

cin>>T;}

for(int i=0;i<T;i++){

cin>>x[i];

}

for(int i=0;i<T;i++){

cout<<factorial(x[i])<<endl;

}
``````

return 0;

}

can any one please provide the right code.
in c++.
i really want to solve this;

This is my submission for This question

NOTE: This question was for values less than 20 only.

``````#include <bits/stdc++.h>
using namespace std;

#define ll long long

void solve()
{
ll int n;
cin >> n;
ll int fact = 1;
for (size_t i = n; i > 0; --i)
{
fact = fact * i;
}
cout << fact<<"\n";
}
int main()
{
ll int t;
cin >> t;
do
{
solve();
t--;
} while (t != 0);

return 0;
}
``````

For finding factorial of larger values you can visit this link, they have explained it completely.