Home > Net >  How to calculate factorial for integers above 12?
How to calculate factorial for integers above 12?

Time:10-29

For some reason whenever I input a number above 12 it = 0.

Here is my code (I've tried using a long int but that just comes up with random numbers instead):

#include <stdio.h>

int main()
{
{
  int c, n, f = 1;

  printf("Enter a number to calculate its factorial\n");
  scanf("%d", &n);
  for (c = 1; c <= n; c  )
    f = f * c;

    if (n < 0)
        printf("Cannot calculate the factorial of a negative number\n");
    else
        {
            printf("Factorial of %d = %d\n", n, f);   
        }
}

CodePudding user response:

#include <stdio.h>

int main()
{

  unsigned long long c, n, f = 1;

  printf("Enter a number to calculate its factorial\n");
  scanf("%llu", &n);
  for (c = 1; c <= n; c  )
    f = f * c;

    if (n < 0)
        printf("Cannot calculate the factorial of a negative number\n");
    else
        {
            printf("Factorial of %llu = %llu\n", n, f);   
        }
}

CodePudding user response:

Factorials get very large very fast - N! will overflow the native integer types for relatively low values of N.

To calculate any factorial larger than 20!, you will need to use a multi-precision library like GNU MP, like so:

#include <stdio.h>
#include <gmp.h>
#include <assert.h>

unsigned long fac( unsigned long n )
{
  unsigned long ret = 1;
  while( n )
    ret *= n--;
  return ret;
}

int main( void )
{
  for ( unsigned long i = 0; i < 21; i   )
  {
    /**
     * Calculate factorial with native unsigned long type
     */
    printf( "fac(%2lu) = !lu\t", i, fac(i) );
    
    /**
     * Calculate factorial using GMP - note that 99! has
     * 157 digits - that's roughly equivalent to 518 bits. 
     * The largest native integer types currently max out
     * at 64 bits.  
     */
    mpz_t rop;
    mpz_fac_ui( rop, i );
    gmp_printf( "mpz_fac_ui(-) = 7Zd\n", i, rop );
  }

  printf( "------------ limit of unsigned long --------------\n" );

  for ( unsigned long i = 21; i < 100; i   )
  {
    printf( "fac(%2lu) = !lu\t", i, fac(i) );
    
    mpz_t rop;
    mpz_fac_ui( rop, i );
    gmp_printf( "mpz_fac_ui(-) = 7Zd\n", i, rop );
  }

  return 0;
}

Output of the program is:

fac( 0) =                     1 mpz_fac_ui( 0) =                                                                                                                                                             1
fac( 1) =                     1 mpz_fac_ui( 1) =                                                                                                                                                             1
fac( 2) =                     2 mpz_fac_ui( 2) =                                                                                                                                                             2
fac( 3) =                     6 mpz_fac_ui( 3) =                                                                                                                                                             6
fac( 4) =                    24 mpz_fac_ui( 4) =                                                                                                                                                            24
fac( 5) =                   120 mpz_fac_ui( 5) =                                                                                                                                                           120
fac( 6) =                   720 mpz_fac_ui( 6) =                                                                                                                                                           720
fac( 7) =                  5040 mpz_fac_ui( 7) =                                                                                                                                                          5040
fac( 8) =                 40320 mpz_fac_ui( 8) =                                                                                                                                                         40320
fac( 9) =                362880 mpz_fac_ui( 9) =                                                                                                                                                        362880
fac(10) =               3628800 mpz_fac_ui(10) =                                                                                                                                                       3628800
fac(11) =              39916800 mpz_fac_ui(11) =                                                                                                                                                      39916800
fac(12) =             479001600 mpz_fac_ui(12) =                                                                                                                                                     479001600
fac(13) =            6227020800 mpz_fac_ui(13) =                                                                                                                                                    6227020800
fac(14) =           87178291200 mpz_fac_ui(14) =                                                                                                                                                   87178291200
fac(15) =         1307674368000 mpz_fac_ui(15) =                                                                                                                                                 1307674368000
fac(16) =        20922789888000 mpz_fac_ui(16) =                                                                                                                                                20922789888000
fac(17) =       355687428096000 mpz_fac_ui(17) =                                                                                                                                               355687428096000
fac(18) =      6402373705728000 mpz_fac_ui(18) =                                                                                                                                              6402373705728000
fac(19) =    121645100408832000 mpz_fac_ui(19) =                                                                                                                                            121645100408832000
fac(20) =   2432902008176640000 mpz_fac_ui(20) =                                                                                                                                           2432902008176640000
------------ limit of unsigned long --------------
fac(21) =  14197454024290336768 mpz_fac_ui(21) =                                                                                                                                          51090942171709440000
fac(22) =  17196083355034583040 mpz_fac_ui(22) =                                                                                                                                        1124000727777607680000
fac(23) =   8128291617894825984 mpz_fac_ui(23) =                                                                                                                                       25852016738884976640000
fac(24) =  10611558092380307456 mpz_fac_ui(24) =                                                                                                                                      620448401733239439360000
fac(25) =   7034535277573963776 mpz_fac_ui(25) =                                                                                                                                    15511210043330985984000000
fac(26) =  16877220553537093632 mpz_fac_ui(26) =                                                                                                                                   403291461126605635584000000
fac(27) =  12963097176472289280 mpz_fac_ui(27) =                                                                                                                                 10888869450418352160768000000
fac(28) =  12478583540742619136 mpz_fac_ui(28) =                                                                                                                                304888344611713860501504000000
fac(29) =  11390785281054474240 mpz_fac_ui(29) =                                                                                                                               8841761993739701954543616000000
fac(30) =   9682165104862298112 mpz_fac_ui(30) =                                                                                                                             265252859812191058636308480000000
fac(31) =   4999213071378415616 mpz_fac_ui(31) =                                                                                                                            8222838654177922817725562880000000
fac(32) =  12400865694432886784 mpz_fac_ui(32) =                                                                                                                          263130836933693530167218012160000000
fac(33) =   3400198294675128320 mpz_fac_ui(33) =                                                                                                                         8683317618811886495518194401280000000
fac(34) =   4926277576697053184 mpz_fac_ui(34) =                                                                                                                       295232799039604140847618609643520000000
fac(35) =   6399018521010896896 mpz_fac_ui(35) =                                                                                                                     10333147966386144929666651337523200000000
fac(36) =   9003737871877668864 mpz_fac_ui(36) =                                                                                                                    371993326789901217467999448150835200000000
fac(37) =   1096907932701818880 mpz_fac_ui(37) =                                                                                                                  13763753091226345046315979581580902400000000
fac(38) =   4789013295250014208 mpz_fac_ui(38) =                                                                                                                 523022617466601111760007224100074291200000000
fac(39) =   2304077777655037952 mpz_fac_ui(39) =                                                                                                               20397882081197443358640281739902897356800000000
fac(40) =  18376134811363311616 mpz_fac_ui(40) =                                                                                                              815915283247897734345611269596115894272000000000
fac(41) =  15551764317513711616 mpz_fac_ui(41) =                                                                                                            33452526613163807108170062053440751665152000000000
fac(42) =   7538058755741581312 mpz_fac_ui(42) =                                                                                                          1405006117752879898543142606244511569936384000000000
fac(43) =  10541877243825618944 mpz_fac_ui(43) =                                                                                                         60415263063373835637355132068513997507264512000000000
fac(44) =   2673996885588443136 mpz_fac_ui(44) =                                                                                                       2658271574788448768043625811014615890319638528000000000
fac(45) =   9649395409222631424 mpz_fac_ui(45) =                                                                                                     119622220865480194561963161495657715064383733760000000000
fac(46) =   1150331055211806720 mpz_fac_ui(46) =                                                                                                    5502622159812088949850305428800254892961651752960000000000
fac(47) =  17172071447535812608 mpz_fac_ui(47) =                                                                                                  258623241511168180642964355153611979969197632389120000000000
fac(48) =  12602690238498734080 mpz_fac_ui(48) =                                                                                                12413915592536072670862289047373375038521486354677760000000000
fac(49) =   8789267254022766592 mpz_fac_ui(49) =                                                                                               608281864034267560872252163321295376887552831379210240000000000
fac(50) =  15188249005818642432 mpz_fac_ui(50) =                                                                                             30414093201713378043612608166064768844377641568960512000000000000
fac(51) =  18284192274659147776 mpz_fac_ui(51) =                                                                                           1551118753287382280224243016469303211063259720016986112000000000000
fac(52) =   9994050523088551936 mpz_fac_ui(52) =                                                                                          80658175170943878571660636856403766975289505440883277824000000000000
fac(53) =  13175843659825807360 mpz_fac_ui(53) =                                                                                        4274883284060025564298013753389399649690343788366813724672000000000000
fac(54) =  10519282829630636032 mpz_fac_ui(54) =                                                                                      230843697339241380472092742683027581083278564571807941132288000000000000
fac(55) =   6711489344688881664 mpz_fac_ui(55) =                                                                                    12696403353658275925965100847566516959580321051449436762275840000000000000
fac(56) =   6908521828386340864 mpz_fac_ui(56) =                                                                                   710998587804863451854045647463724949736497978881168458687447040000000000000
fac(57) =   6404118670120845312 mpz_fac_ui(57) =                                                                                 40526919504877216755680601905432322134980384796226602145184481280000000000000
fac(58) =   2504001392817995776 mpz_fac_ui(58) =                                                                               2350561331282878571829474910515074683828862318181142924420699914240000000000000
fac(59) =    162129586585337856 mpz_fac_ui(59) =                                                                             138683118545689835737939019720389406345902876772687432540821294940160000000000000
fac(60) =   9727775195120271360 mpz_fac_ui(60) =                                                                            8320987112741390144276341183223364380754172606361245952449277696409600000000000000
fac(61) =   3098476543630901248 mpz_fac_ui(61) =                                                                          507580213877224798800856812176625227226004528988036003099405939480985600000000000000
fac(62) =   7638104968020361216 mpz_fac_ui(62) =                                                                        31469973260387937525653122354950764088012280797258232192163168247821107200000000000000
fac(63) =   1585267068834414592 mpz_fac_ui(63) =                                                                      1982608315404440064116146708361898137544773690227268628106279599612729753600000000000000
fac(64) =   9223372036854775808 mpz_fac_ui(64) =                                                                    126886932185884164103433389335161480802865516174545192198801894375214704230400000000000000
fac(65) =   9223372036854775808 mpz_fac_ui(65) =                                                                   8247650592082470666723170306785496252186258551345437492922123134388955774976000000000000000
fac(66) =                     0 mpz_fac_ui(66) =                                                                 544344939077443064003729240247842752644293064388798874532860126869671081148416000000000000000
fac(67) =                     0 mpz_fac_ui(67) =                                                               36471110918188685288249859096605464427167635314049524593701628500267962436943872000000000000000
fac(68) =                     0 mpz_fac_ui(68) =                                                             2480035542436830599600990418569171581047399201355367672371710738018221445712183296000000000000000
fac(69) =                     0 mpz_fac_ui(69) =                                                           171122452428141311372468338881272839092270544893520369393648040923257279754140647424000000000000000
fac(70) =                     0 mpz_fac_ui(70) =                                                         11978571669969891796072783721689098736458938142546425857555362864628009582789845319680000000000000000
fac(71) =                     0 mpz_fac_ui(71) =                                                        850478588567862317521167644239926010288584608120796235886430763388588680378079017697280000000000000000
fac(72) =                     0 mpz_fac_ui(72) =                                                      61234458376886086861524070385274672740778091784697328983823014963978384987221689274204160000000000000000
fac(73) =                     0 mpz_fac_ui(73) =                                                    4470115461512684340891257138125051110076800700282905015819080092370422104067183317016903680000000000000000
fac(74) =                     0 mpz_fac_ui(74) =                                                  330788544151938641225953028221253782145683251820934971170611926835411235700971565459250872320000000000000000
fac(75) =                     0 mpz_fac_ui(75) =                                                24809140811395398091946477116594033660926243886570122837795894512655842677572867409443815424000000000000000000
fac(76) =                     0 mpz_fac_ui(76) =                                              1885494701666050254987932260861146558230394535379329335672487982961844043495537923117729972224000000000000000000
fac(77) =                     0 mpz_fac_ui(77) =                                            145183092028285869634070784086308284983740379224208358846781574688061991349156420080065207861248000000000000000000
fac(78) =                     0 mpz_fac_ui(78) =                                          11324281178206297831457521158732046228731749579488251990048962825668835325234200766245086213177344000000000000000000
fac(79) =                     0 mpz_fac_ui(79) =                                         894618213078297528685144171539831652069808216779571907213868063227837990693501860533361810841010176000000000000000000
fac(80) =                     0 mpz_fac_ui(80) =                                       71569457046263802294811533723186532165584657342365752577109445058227039255480148842668944867280814080000000000000000000
fac(81) =                     0 mpz_fac_ui(81) =                                     5797126020747367985879734231578109105412357244731625958745865049716390179693892056256184534249745940480000000000000000000
fac(82) =                     0 mpz_fac_ui(82) =                                   475364333701284174842138206989404946643813294067993328617160934076743994734899148613007131808479167119360000000000000000000
fac(83) =                     0 mpz_fac_ui(83) =                                 39455239697206586511897471180120610571436503407643446275224357528369751562996629334879591940103770870906880000000000000000000
fac(84) =                     0 mpz_fac_ui(84) =                               3314240134565353266999387579130131288000666286242049487118846032383059131291716864129885722968716753156177920000000000000000000
fac(85) =                     0 mpz_fac_ui(85) =                             281710411438055027694947944226061159480056634330574206405101912752560026159795933451040286452340924018275123200000000000000000000
fac(86) =                     0 mpz_fac_ui(86) =                           24227095383672732381765523203441259715284870552429381750838764496720162249742450276789464634901319465571660595200000000000000000000
fac(87) =                     0 mpz_fac_ui(87) =                         2107757298379527717213600518699389595229783738061356212322972511214654115727593174080683423236414793504734471782400000000000000000000
fac(88) =                     0 mpz_fac_ui(88) =                       185482642257398439114796845645546284380220968949399346684421580986889562184028199319100141244804501828416633516851200000000000000000000
fac(89) =                     0 mpz_fac_ui(89) =                     16507955160908461081216919262453619309839666236496541854913520707833171034378509739399912570787600662729080382999756800000000000000000000
fac(90) =                     0 mpz_fac_ui(90) =                   1485715964481761497309522733620825737885569961284688766942216863704985393094065876545992131370884059645617234469978112000000000000000000000
fac(91) =                     0 mpz_fac_ui(91) =                 135200152767840296255166568759495142147586866476906677791741734597153670771559994765685283954750449427751168336768008192000000000000000000000
fac(92) =                     0 mpz_fac_ui(92) =               12438414054641307255475324325873553077577991715875414356840239582938137710983519518443046123837041347353107486982656753664000000000000000000000
fac(93) =                     0 mpz_fac_ui(93) =             1156772507081641574759205162306240436214753229576413535186142281213246807121467315215203289516844845303838996289387078090752000000000000000000000
fac(94) =                     0 mpz_fac_ui(94) =           108736615665674308027365285256786601004186803580182872307497374434045199869417927630229109214583415458560865651202385340530688000000000000000000000
fac(95) =                     0 mpz_fac_ui(95) =         10329978488239059262599702099394727095397746340117372869212250571234293987594703124871765375385424468563282236864226607350415360000000000000000000000
fac(96) =                     0 mpz_fac_ui(96) =        991677934870949689209571401541893801158183648651267795444376054838492222809091499987689476037000748982075094738965754305639874560000000000000000000000
fac(97) =                     0 mpz_fac_ui(97) =      96192759682482119853328425949563698712343813919172976158104477319333745612481875498805879175589072651261284189679678167647067832320000000000000000000000
fac(98) =                     0 mpz_fac_ui(98) =    9426890448883247745626185743057242473809693764078951663494238777294707070023223798882976159207729119823605850588608460429412647567360000000000000000000000
fac(99) =                     0 mpz_fac_ui(99) =  933262154439441526816992388562667004907159682643816214685929638952175999932299156089414639761565182862536979208272237582511852109168640000000000000000000000


    
        

CodePudding user response:

For some reason whenever I input a number above 12 it = 0.

The reason is the largest value a 32-bit signed integer can hold is 231-1 or 2,147,483,647. 13! is 6,227,020,800, so you have exceeded the bounds of a C int.

If your compiler supports it, unsigned long long will get you to 20!:

  • unsigned long long (64-bits) max is 264-1 or 18,446,744,073,709,551,615
  • 20! = 2,432,902,008,176,640,000 (ok)
  • 21! = 51,090,942,171,709,440,000 (too big)

You need a "big integer" library to handle integers bigger than this.

Or, use Python

  • Related