public class Test07 {
/*
* 1!+2!+3!+4!+5!=?
5!= 1*2*3*4*5;最小为1,最大为5
1 n= 1x1
2 n=1x1x2
3 n=1x1x2x3
4 n=1x1x2x3x4
5 n=1x1x2x3x4x5
*/
public static void main(String[] args) {
int n = 1;//阶乘的结果
int sum = 0;//定义总和
//循环1至5
for(int i =1;i<=5;i++){
//使用1乘到5
n*=i;
sum+=n;
}
System.out.println(n);
System.out.println("至5的阶乘总和为:"+sum);
}
}
递归
class demo6
{
public static void main(String[] args)
{ int sum=0;
for(int i=0;i<=5;i++)
{
sum+=factorial(i);
}
System.out.println(sum);9
}
public static long factorial(long number) {
if (number <= 1)
return 1;
else
return number * factorial(number - 1);
}
}