将数字相乘后相乘

时间:2013-05-14 19:15:23

标签: numbers each

练习Product1ToN(循环):编写一个名为Product1ToN的程序来计算整数1到10的乘积(即1×2×3×...×10)。尝试计算1到11,1到12,1到13和1到14的产品。记下获得的产品并解释结果。

我怎样才能做到这一点?他们有多种方式?

1 个答案:

答案 0 :(得分:0)

试试这个:

private int Product1ToN(int n){
    int result = 1, ctr = 1;

    while(ctr <= n){
        result *= ctr++;
    }

    return result;
}

希望这有帮助。

相关问题