Factorial of big numbers in c






C program to find out factorial of big or very large numbers 


#include<stdio.h>
#define MAX 10000
void factorialof(int);
void multiply(int);
int length = 0;
int fact[MAX];

int main(){
    int num;
    int i;

    printf("Enter any integer number : ");
    scanf("%d",&num);
   
    fact[0]=1;

    factorialof(num);
   
    printf("Factorial is : ");
    for(i=length;i>=0;i--){
         printf("%d",fact[i]);
    }
    return 0;
}
void factorialof(int num){
    int i;
    for(i=2;i<=num;i++){
         multiply(i);
    }
}
void multiply(int num){
    long i,r=0;
    int arr[MAX];
    for(i=0;i<=length;i++){
                arr[i]=fact[i];
        }

    for(i=0;i<=length;i++){
         fact[i] = (arr[i]*num + r)%10;
         r = (arr[i]*num + r)/10;
         //printf("%d ",r);
    }
    if(r!=0){
         while(r!=0){
             fact[i]=r%10;
             r= r/10;
             i++;
         }
    }
    length = i-1;   
}


Note: You can change the value of MAX to find factorial of too large numbers.


Factorial of some numbers using above c code:

10!:3628800

20!: 2432902008176640000

50!:304140932017133780436126081660647688443776415689605120000

00000000


100!: 

9332621544394415268169923885626670049071596826438162146859296

3895217599993229915608941463976156518286253697920827223758251

18521091686400000000000



500!:12201368259911100687012387854230469262535743428031928421

9241358838584537315388199760549644750220328186301361647714820

3584163378722078177200480785205159329285477907571939330603772

9608590862704291745478824249127263443056701732707694610628023

1045264421887878946575477714986349436778103764427403382736539

7471386477878495438489595537537990423241061271326984327745715

5463099772027810145610811883737095310163563244329870295638966

2891165897476957208792692887128178007026517450776841071962439

0394322536422605234945850129918571501248706961568141625359056

6934238130088562492468915641267756544818865065938479517753608

9400574523894033579847636394490531306232374906644504882466507

5946735862074637925184200459369692981022263971952597190945217

8233317569345815085523328207628200234026269078983424517120062

0771464097945611612762914595123722991334016955236385094288559

2018727433795173014586357570828355780158735432768888680120399

8823847021514676054454076635359841744304801289383138968816394

8746965881750450692636533817505547812864000000000000000000000

0000000000000000000000000000000000000000000000000000000000000

000000000000000000000000000000000000000000

1000!:4023872600770937735437024339230039857193748642107146325

4379991042993851239862902059204420848696940480047998861019719

6058631666872994808558901323829669944590997424504087073759918

8236277271887325197795059509952761208749754624970436014182780

9464649629105639388743788648733711918104582578364784997701247

6632889835955735432513185323958463075557409114262417474349347

5534286465766116677973966688202912073791438537195882498081268

6783837455973174613608537953452422158659320192809087829730843

1392844403281231558611036976801357304216168747609675871348312

0254785893207671691324484262361314125087802080002616831510273

4182797770478463586817016436502415369139828126481021309276124

4896359928705114964975419909342221566832572080821333186116811

5536158365469840467089756029009505376164758477284218896796462

4494516076535340819890138544248798495995331910172335555660213

9450399736280750137837615307127761926849034352625200015888535

1473316117021039681759215109077880193931781141945452572238655

4146106289218796022383897147608850627686296714667469756291123

4082439208160153780889893964518263243671616762179168909779911

9037540312746222899880051954444142820121873617459926429565817

4662830295557029902432415318161721046583203678690611726015878

3520751516284225540265170483304226143974286933061690897968482

5901254583271682264580665267699586526822728070757813918581788

8965220816434834482599326604336766017699961283186078838615027

9465955131156552036093988180612138558600301435694527224206344

6317974605946825731037900840244324384656572450144028218852524

7093519062092902313649327349756551395872055965422874977401141

3346962715422845862377387538230483865688976461927383814900140

7673104466402598994902222217659043399018860185665264850617997

0235619389701786004081188972991831102117122984590164192106888

4387121855646124960798722908519296819372388642614839657382291

1231250241866493531439701374285319266498753372189406942814341

1852015801412334482801505139969429015348307764456909907315243

3278288269864602789864321139083506217095002597389863554277196

7428222487575867657523442202075736305694988250879689281627538

4886339690995982628095612145099487170124451646126037902930912

0889086942028510640182154399457156805941872748998094254742173

5824010636774045957417851608292301353580818400969963725242305

6085590370062427124341690900415369010593398383577793941097002

7753472000000000000000000000000000000000000000000000000000000

0000000000000000000000000000000000000000000000000000000000000

0000000000000000000000000000000000000000000000000000000000000

0000000000000000000000000000000000000000000000000000000000000

000000000000  




2 comments:

  1. can u send me the algorithm for this program

    ReplyDelete
  2. the results for 50 and for 1000 are wrong by a factor of 2.
    my results:
    10 3.6288e+06
    20 2.4329e+19
    50 6.08282e+66
    100 9.33262e+161
    500 1.22014e+1140
    1000 2.01194e+2576

    ReplyDelete