- package One;
- /**
- * 求阶乘递归算法
- * @author WuPeng
- *
- */
- public class FactorialDemo {
- /**
- * 计算一个数的阶乘:n!
- * @param n
- * @return
- */
- public static int fact(int n) {
- if(n == 0)
- return 1;
- else
- return fact(n-1)*n;
- }
-
- public static void main(String[] args) {
- /*
- * 求1!+2!+3!+……+10!的和
- */
- int sum = 0;
- for (int i = 0; i < 10; i++) {
- sum += fact(i);
- }
- System.out.println("1!+2!+3!+……+10!=" + sum);
- }
- }
复制代码 |