码迷,mamicode.com
首页 > 其他好文 > 详细

求1+2!+3!+...+20!的和。

时间:2017-04-13 21:17:03      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:image   .com   阶乘   topic   ges   http   类型   style   bsp   

package com.liron.p1;

/**求1+2!+3!+...+20!的和*/
public class Topic25 {
    public static void main(String[] args){
        
        long sum=0;//因为数太大要定义成long类型
        long n=1;
        int i=1;
        
        for (i=1; i<=20 ;i++ ){
            n*=i;//此处是计算每个项的阶乘
            sum+=n;//此处是把每个项的阶乘数依次相加
        }
        System.out.println("所有阶乘数的和为:"+sum);
    }
}

结果:

技术分享

 

求1+2!+3!+...+20!的和。

标签:image   .com   阶乘   topic   ges   http   类型   style   bsp   

原文地址:http://www.cnblogs.com/llr211/p/6705768.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!