/* my code fore the program small factorial */
/move to bottom/
#include<stdio.h>
double FACT(short int n) ;
int main()
{
short int X,i=0;
int NUM[100];
scanf("%d",&X);
if(1<=X<=100){
while(i<X){
scanf("%d",&NUM[i]);
i++;
}
}
i=0;
while(i<X){
if(1<=(NUM[i])<=100){
printf("%.0lf\n",FACT(NUM[i]));
}
i++;
}
return 0;
}
double FACT(short int n){
short int i;
double sum=1;
for(i=1;i<=n;i++){
sum*=i;
}
return(sum);
}
/*When i tried to submit it, Compiler says Wrong Answer. Can u figure out the problem.......please....*/
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…
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…
Previous answers are correct, see your code at IdeOne http://ideone.com/0cY3Y4 (test #2), for number 25 it returns 15511210043330986055303168, but x! ends with 0 for x >= 5.
@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