Pls take a look at my program for Small Factorials, it shows Wrong Answer and I dont know why?
#include<stdio.h>
int main()
{int i,n,l,fact[100],t;
scanf("%d",&t);
if(t>=1&&t<=100)
{for(l=0;l<100;l++)
{fact[l]=1;}
l=0;
while(t>0)
{scanf("%d",&n);
if(n>=1&&n<=100)
{for(i=n;i>0;i–)
{fact[l]=fact[l]*i;}
printf("%d\n",fact[l]);
l++;
t–;
}}}
return 0;
}
The logic to compute factorial is correct. However you need to realize that the values of factorials grows very fast and 100! is very very big :
9332621544394415268169923885626670049071596826438162146859296389521759999322…
9915608941463976156518286253697920827223758251185210916864000000000000000000000000!!!
This value cannot fit in the normal int or even long long data type in C or C++.There are 2 alternatives to solve this:
- Use a language like Java or Python with inbuilt support to handle large numbers
2)Store the individual digits of the number in an array and perform operations on this array. Here is a wonderful tutorial by @kurma on this which explains how to handle large numbers in C and C++.Good luck