相关疑难解决方法(0)

计算C中的大数因子

在我的C代码中,我想计算1到100范围内的数字的阶乘.对于小数字,该函数可以工作,但对于更大的数字,例如100!它返回不正确的结果.有什么方法可以处理C中的大数阶因子?我正在使用的编译器是gcc v4.3.3.我的代码如下:

#include <stdio.h>
#include <math.h>

double print_solution(int);

int main(void)
{
        int no_of_inputs,n ;

        int ctr = 1;

        scanf("%d",&no_of_inputs); //Read no of inputs

        do
        {
                scanf("%d",&n); //Read the input

                printf("%.0f\n",print_solution(n));

                ctr++;  

        }while(ctr <= no_of_inputs);


        return 0;       
}

double print_solution(int n)
{
        if(n == 0 || n == 1)
                return 1;
        else
                return n*print_solution(n-1);


}
Run Code Online (Sandbox Code Playgroud)

c algorithm

19
推荐指数
6
解决办法
5万
查看次数

标签 统计

algorithm ×1

c ×1