Java-01,計算1-10的階乘之和


public class HelloWorld{
    public static void main(String[] args) {
        long result = 0;
        long f = 1;
        for(int i = 1; i <= 10; i++){
            f = f * i;
            System.out.printf("i = %d\n f = %d\n",i,f);
            result += f;
            System.out.printf("result = %d\n",result);
        }
        System.out.printf("result = %d\n",result);
    }
}

執行結果:

i = 1
f = 1
result = 1
i = 2
f = 2
result = 3
i = 3
f = 6
result = 9
i = 4
f = 24
result = 33
i = 5
f = 120
result = 153
i = 6
f = 720
result = 873
i = 7
f = 5040
result = 5913
i = 8
f = 40320
result = 46233
i = 9
f = 362880
result = 409113
i = 10
f = 3628800
result = 4037913
result = 4037913


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM