Search code examples
javafactorial

i want to calculate factorial of 50 with my java program without using array


here is my program it

`class Factorial
{
public static void main(String dt[])
{
    long n=50;
    long fact=1;
    if(n==0)
    {
        System.out.println("Factorial is 1");
    }
    else if(n==1)
    {
        System.out.println("Factorial is 1");
    }
    else
    {
        fact=n;
        while(n>1)
        {
            fact=fact*(n-1);
            n--;
        }
        System.out.print("Factorial is "+fact);
    }
}
}`

it doesnt calculate factorial of 50.i want to calculate it without using arrays and big integer


Solution

  • factorial is a math function that grows very fast to infinite, Your code is wrong because the using longs is to short for that range (by 21! you will have already an overflow)

    you need to use another data type, like big integer.

    a refactored method using BigInteger will looks like:

    private static void factorial(long n) {
        long x = n;
        BigInteger fact = BigInteger.ONE;
        if ((n == 0) || (n == 1)) {
            System.out.println("Factorial is 1");
        } else {
            fact = BigInteger.valueOf(n);
            while (n > 1) {
                fact = fact.multiply(BigInteger.valueOf(n - 1));
                n--;
            }
            System.out.println("Factorial of " + x + " is " + fact);
        }
    }
    

    note the output from 21! until 50! that can be verified online here

    Factorial of 21 is 51090942171709440000

    Factorial of 22 is 1124000727777607680000

    Factorial of 23 is 25852016738884976640000

    Factorial of 24 is 620448401733239439360000

    Factorial of 25 is 15511210043330985984000000

    Factorial of 26 is 403291461126605635584000000

    Factorial of 27 is 10888869450418352160768000000

    Factorial of 28 is 304888344611713860501504000000

    Factorial of 29 is 8841761993739701954543616000000

    Factorial of 30 is 265252859812191058636308480000000

    Factorial of 31 is 8222838654177922817725562880000000

    Factorial of 32 is 263130836933693530167218012160000000

    Factorial of 33 is 8683317618811886495518194401280000000

    Factorial of 34 is 295232799039604140847618609643520000000

    Factorial of 35 is 10333147966386144929666651337523200000000

    Factorial of 36 is 371993326789901217467999448150835200000000

    Factorial of 37 is 13763753091226345046315979581580902400000000

    Factorial of 38 is 523022617466601111760007224100074291200000000

    Factorial of 39 is 20397882081197443358640281739902897356800000000

    Factorial of 40 is 815915283247897734345611269596115894272000000000

    Factorial of 41 is 33452526613163807108170062053440751665152000000000

    Factorial of 42 is 1405006117752879898543142606244511569936384000000000

    Factorial of 43 is 60415263063373835637355132068513997507264512000000000

    Factorial of 44 is 2658271574788448768043625811014615890319638528000000000

    Factorial of 45 is 119622220865480194561963161495657715064383733760000000000

    Factorial of 46 is 5502622159812088949850305428800254892961651752960000000000

    Factorial of 47 is 258623241511168180642964355153611979969197632389120000000000

    Factorial of 48 is 12413915592536072670862289047373375038521486354677760000000000

    Factorial of 49 is 608281864034267560872252163321295376887552831379210240000000000

    Factorial of 50 is 30414093201713378043612608166064768844377641568960512000000000000