#include using namespace std; int main() { int t; int n; int x; cin >> t; while(t) { cin >> n; x = 1; for(int i = 2; i <= n; i++) { x *= i; } cout << x << endl; t--; } return 0; }
@yusufmm : You are getting wrong answer because of integer overflow .
Factorials of values upto 100 are required in this problem and you can’t store them in 32-bit or 64-bit numbers .
Make a test file with 100 test cases with the i’th test case being number i . You will notice several negative numbers in your output .
You need to make a BigInteger class which can work with arbitrarily large numbers .
#include<stdio.h>
#define MAX 100
int fact(int);
int main()
{
int t,i,n,out[MAX];
scanf("%d\n",&t);
for(i=0;i<t;i++)
{
scanf("%d\n",&n);
out[i]=fact(n);
}
for(i=0;i<t;i++)
{
printf("%d\n",&out[i]);
}
return 0;
}
int fact(int x)
{
int f=1,i;
for(i=1;i<x;i++)
{
f=f*i;
}
return f;
}
whats the problem with this?
The reason for your wrong answer is the same as stated by @vineetpaliwal above, that is, integer overflow. Apply some logic on array to store the large number obtained upon multiplication.
And I would also point out that **printf("%d\n",&out[i]);** should be **printf("%d\n",out[i]);**