Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***************************************
- factorial of huge nos
- correction: huge factorials of nos less than 100.
- Shaha Hassan
- Jan19, 2014
- Note: delete the lines commented "conio" if you are on a compiler which does not support conio(any modern compiler)
- ******************************************/
- #include<stdio.h>
- #include<conio.h>
- #define MXP 170
- #define MXN 6
- void s2i(char *c, int *n);
- void i2s(int *n, char *c);
- void show(int *n);
- void get(int *n);
- void mul(int *n1, int *n2, int *product);
- int isZero(int *n);
- void copy(int *dst, int *src);
- void dec(int *n);
- void main()
- {
- int n[MXN], tmp[MXP], pro[MXP];
- clrscr(); //conio
- printf("\nEnter number: ");
- get(n);
- pro[0] =pro[1] = 1;
- while( !isZero(n) ) {
- copy(tmp, pro);
- mul(tmp,n,pro);
- dec(n);
- }
- printf("FActorial is ");
- show(pro);
- getch(); //conio
- }
- void dec(int *n) {
- int i=1;
- if( n[1] != 0 ) { //non zero unit place
- if( (--n[1]==0) && (n[0]==1) ) {
- n[0] = 0;
- }
- }else{ //no ends in 0s
- i=1;
- while( n[i] ==0 ) {
- n[i++] = 9;
- }
- n[i]--;
- //sync size or n[0]
- if(n[n[0]] == 0) {
- n[0]--;
- }
- }
- }
- void copy(int *d, int *s) {
- int i;
- for(i=0; i<=s[0]; i++) {
- d[i]= s[i];
- }
- }
- int isZero(int *n) {
- return !(n[0]);
- }
- void mul(int *v, int *r, int *p) {
- int i,j, k;
- for(i=0; i<MXP; p[i++]=0);
- for(i=1; i<=r[0]; i++) {
- for(j=1; j<=v[0]; j++) {
- p[i+j-1] += v[j] * r[i];
- if( p[i+j-1] > 9) {
- p[i+j] += p[i+j-1] /10;
- p[i+j-1] %= 10;
- }
- }
- }
- k = v[0]+r[0]+1;
- while( p[k--]==0);
- p[0]=k+1;
- }
- void get(int *n)
- {
- char buf[MXN];
- scanf("%s",buf);
- if(strlen(buf)> MXN) {
- printf("too big. Try again");
- get(n);
- return;
- } else {
- strrev(buf);
- s2i(buf,n);
- }
- }
- void show(int *n)
- {
- char buf[MXP];
- i2s(n,buf);
- strrev(buf);
- printf("%s = %d.%de+%d",buf,n[n[0]], n[n[0]-1], n[0]-1);
- }
- void s2i(char *c, int *n)
- {
- int i;
- n[0] = strlen(c);
- for(i=0; i<n[0]; i++) {
- n[i+1] = c[i] - '0';
- }
- }
- void i2s(int *n, char *c)
- {
- int i;
- for(i=1; i<=n[0]; i++) {
- c[i-1] = n[i] +'0';
- }
- c[i-1] = '\0';
- }
- //end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement