例4-7 求1!+2!+…+100!

例4-7 求1!+2!+…+100!

计算 1!+2!+…+100!
程序核心——两个循环的镶嵌

程序


#include<stdio.h>
int main()
{
    int i,j,n;
    double sum,result;
    sum=0;
    for(i=1;i<=100;i++)
    {
        result=1;
        for(j=1;j<=i;j++)
            result*=j;
        sum=sum+result;
    }
    printf("1!+2!+…+100!=%e\n",sum); 
    return 0;
 } 

结果

1!+2!+…+100!=9.426900e+157

--------------------------------
Process exited after 0.2632 seconds with return value 0
请按任意键继续. . .

分析

重点:与4-6相比未使用自定义函数

猜你喜欢

转载自www.cnblogs.com/5236288kai/p/10660803.html