Advertisement
BinYamin

Factorial 1-100

Jan 19th, 2014
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.25 KB | None | 0 0
  1. /***************************************
  2.    factorial of huge nos
  3.    correction: huge factorials of nos less than 100.
  4.      Shaha Hassan
  5.      Jan19, 2014
  6.    Note: delete the lines commented "conio" if you are on a compiler which does not support conio(any modern compiler)
  7. ******************************************/
  8. #include<stdio.h>
  9. #include<conio.h>
  10. #define MXP 170
  11. #define MXN 6
  12.  
  13. void s2i(char *c, int *n);
  14. void i2s(int *n, char *c);
  15. void show(int *n);
  16. void get(int *n);
  17. void mul(int *n1, int *n2, int *product);
  18. int isZero(int *n);
  19. void copy(int *dst, int *src);
  20. void dec(int *n);
  21.  
  22. void main()
  23. {
  24.     int n[MXN], tmp[MXP], pro[MXP];
  25.  
  26.     clrscr();       //conio
  27.     printf("\nEnter number: ");
  28.     get(n);
  29.     pro[0] =pro[1] = 1;
  30.     while( !isZero(n) ) {
  31.     copy(tmp, pro);
  32.     mul(tmp,n,pro);
  33.     dec(n);
  34.     }
  35.  
  36.     printf("FActorial is  ");
  37.     show(pro);
  38.     getch();        //conio
  39. }
  40.  
  41. void dec(int *n) {
  42.     int i=1;
  43.     if( n[1] != 0 ) {  //non zero unit place
  44.     if( (--n[1]==0) && (n[0]==1) ) {
  45.         n[0] = 0;
  46.     }
  47.     }else{             //no ends in 0s
  48.     i=1;
  49.     while( n[i] ==0 ) {
  50.         n[i++] = 9;
  51.     }
  52.     n[i]--;
  53.     //sync size or n[0]
  54.     if(n[n[0]] == 0) {
  55.         n[0]--;
  56.     }
  57.     }
  58. }
  59.  
  60. void copy(int *d, int *s) {
  61.     int i;
  62.     for(i=0; i<=s[0]; i++) {
  63.     d[i]= s[i];
  64.     }
  65. }
  66.  
  67. int isZero(int *n) {
  68.     return !(n[0]);
  69. }
  70.  
  71. void mul(int *v, int *r, int *p) {
  72.     int i,j, k;
  73.     for(i=0; i<MXP; p[i++]=0);
  74.  
  75.     for(i=1; i<=r[0]; i++) {
  76.     for(j=1; j<=v[0]; j++) {
  77.         p[i+j-1] += v[j] * r[i];
  78.         if( p[i+j-1] > 9) {
  79.         p[i+j] += p[i+j-1] /10;
  80.         p[i+j-1] %= 10;
  81.         }
  82.     }
  83.     }
  84.     k = v[0]+r[0]+1;
  85.     while( p[k--]==0);
  86.     p[0]=k+1;
  87. }
  88.  
  89. void get(int *n)
  90. {
  91.     char buf[MXN];
  92.     scanf("%s",buf);
  93.     if(strlen(buf)> MXN) {
  94.     printf("too big. Try again");
  95.     get(n);
  96.     return;
  97.     } else {
  98.     strrev(buf);
  99.     s2i(buf,n);
  100.     }
  101. }
  102.  
  103. void show(int *n)
  104. {
  105.     char buf[MXP];
  106.     i2s(n,buf);
  107.     strrev(buf);
  108.     printf("%s = %d.%de+%d",buf,n[n[0]], n[n[0]-1], n[0]-1);
  109. }
  110.  
  111. void s2i(char *c, int *n)
  112. {
  113.     int i;
  114.     n[0] = strlen(c);
  115.     for(i=0; i<n[0]; i++) {
  116.     n[i+1] = c[i] - '0';
  117.     }
  118. }
  119.  
  120. void i2s(int *n, char *c)
  121. {
  122.     int i;
  123.     for(i=1; i<=n[0]; i++) {
  124.     c[i-1] = n[i] +'0';
  125.     }
  126.     c[i-1] = '\0';
  127. }
  128. //end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement