1 #include<stdio.h> 2 #define N 1000 3 4 int Factorial(int n, int a[], int index); 5 int main() 6 { 7 int a[N] = {0,1},index = 1; 8 for(int n = 1; n<=100; ++n) 9 { 10 index = Factorial(n, a, index); 11 printf("%d! = ", n); 12 for(int i=index; i>0; i--) 13 { 14 printf("%d", a[i]); 15 } 16 printf("\n"); 17 } 18 19 return 0; 20 } 21 22 int Factorial(int n, int a[], int index) 23 { 24 int carry = 0;/**< 进位数 */ 25 for(int j=1; j<=index; j++)/**< 乘数与被乘数,从第一位开始,逐位相乘 */ 26 { 27 int temp = n*a[j] + carry;/**< 乘积 + 进位数 */ 28 a[j] = temp%10;/**< 乘积 + 进位数,保留个位 */ 29 carry = temp/10;/**< 进位 */ 30 } 31 while(carry > 0)/**< 进位数大于0,最高位++,最高位保留进位数个位 */ 32 { 33 a[++index] = carry%10; 34 carry /= 10; 35 } 36 return index; 37 }
1 #include <stdio.h> 2 int main() 3 { 4 int n,j,i,t,p; 5 int FACT[10000]; 6 while(scanf("%d",&n)!=EOF) 7 { 8 t=p=0; 9 FACT[0]=1; 10 for(j=1;j<=n;j++) 11 { 12 for(i=0;i<=t;i++){ 13 FACT[i]=FACT[i]*j+p; 14 p=FACT[i]/100000; 15 FACT[i]%=100000; 16 } 17 if(p){ 18 t++; 19 FACT[t]=p; 20 p=0; 21 } 22 } 23 printf("%d",FACT[t]); 24 for(i=t-1;i>=0;i--) 25 printf("%05d",FACT[i]); 26 printf("\n"); 27 } 28 return 0; 29 }