small factorials

/* my code fore the program small factorial */
/move to bottom/

double FACT(short int n) ;
int main()
short int X,i=0;
int NUM[100];
return 0;

double FACT(short int n){
short int i;
double sum=1;

/*When i tried to submit it, Compiler says Wrong Answer. Can u figure out the problem.......please....*/

Hello @neel123,

The factorial of a number grows very quickly with its argument and by around 20/21! it is not possible to represent this number using built-in data types present in C++, you must use grade school arithmetic and an array-like representation of the number to obtain correct answer…

You can read my tutorial for this problem, here.

I hope this helps you out.

Best regards,


double can surely hold such large numbers, but with loss of precision…cause of this!!!

Hint:- u need to use an array to create a sort of data type…which will be able to store such huge values with 100% precision…also u can use python…its int can store huge values…hope this helps…:slight_smile:

Previous answers are correct, see your code at IdeOne (test #2), for number 25 it returns 15511210043330986055303168, but x! ends with 0 for x >= 5.

@neel123 be aware that this code:


is not doing what you think, correctly it is

if( 1 <= NUM[i] && NUM[i] <= 100 )

Questions for others: Do you have an idea why that code fails in runtime when C++ is used instead of C (

@neel123 first, you are using double to store the factorial of the numbers but, it only works for the samller value. Second, read its editorial which is well written and you understand where you are going wrong.

@betlista there is a special thing going on that program. In the program, X is declared as short int which has a size of only 2 bytes. Whereas it is readed from the standard input as integer through scanf("%d",&X). And it is well known the size of integer is 4 bytes. Which is the cause of runtime memory violation.

Instead of using %d use %hd like scanf("%hd",&X). It works fine. Here is the


1 Like

if you can provide original statement, it will help

also I don’t understand why you use double, when answer is allways integer and I don’t know the limit for elements of NUM

@michal27 you can find easily from user’s profile page

1 Like