Storing a number greater than 20! (factorial)

后端 未结 4 1002
面向向阳花
面向向阳花 2021-01-12 05:33

I am trying to find the numbers till 100! (factorial) but after 20! it gives an error as the value is too large to handle. How can I store such a

4条回答
  •  别那么骄傲
    2021-01-12 06:07

    This code will help up to 170!. Use double for the factorial function, because normal int or long int will not support that factorial result, double will support up to 1.7e308.

    #include
    
    double fact(int k){
        int i;
        double f=1;
        for(i=1;i<=k;i++) f=f*i;
        return (f);
    }
    int main(){
        int i;
        for(i=1;i<=65;i++)
        printf("%d! = %.3lf\n",i,fact(i));
        return 0;
    }
    

提交回复
热议问题