编写一个JAVA程序,求1!+2!+3!+......+10!的结果,并将结果输出
6个回答
展开全部
效率阿。。。如果超过17的阶乘,就要用long了
public class Factorial {
public static void main(String[] args) {
int sum = 0;
int fac = 1;
for(int i = 1; i <= 10; i++){
fac = i * fac;
System.out.println(i + "! = " + fac);
sum += fac;
}
System.out.println("1!+2!+3!+...+10! = " + sum);
}
}
--------------
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
1!+2!+3!+...+10! = 4037913
public class Factorial {
public static void main(String[] args) {
int sum = 0;
int fac = 1;
for(int i = 1; i <= 10; i++){
fac = i * fac;
System.out.println(i + "! = " + fac);
sum += fac;
}
System.out.println("1!+2!+3!+...+10! = " + sum);
}
}
--------------
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
1!+2!+3!+...+10! = 4037913
展开全部
笨办法,但是好理解。
public class test
{
public static int jiecheng(int n)
{
int sum = 1;
for (int i = 1; i <= n; i++)
{
sum = i*sum;
}
return sum;
}
public static void main(String [ ] args)
{
test t = new test();
int all=0;
for(int i=1;i<=10;i++)
{
all=all+jiecheng(i);
System.out.println("总数是:"+all);
}
//System.out.println("总数是:"+all);
}
}
public class test
{
public static int jiecheng(int n)
{
int sum = 1;
for (int i = 1; i <= n; i++)
{
sum = i*sum;
}
return sum;
}
public static void main(String [ ] args)
{
test t = new test();
int all=0;
for(int i=1;i<=10;i++)
{
all=all+jiecheng(i);
System.out.println("总数是:"+all);
}
//System.out.println("总数是:"+all);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Factorial {
private int result = 1;
public int factorial(int number){
if(number==1)
return result;
else
{
result = number*factorial(number-1);
return result;
}
}
public static void main(String[] args) {
Factorial f = new Factorial();
System.out.println(f.factorial(10));
}
}
private int result = 1;
public int factorial(int number){
if(number==1)
return result;
else
{
result = number*factorial(number-1);
return result;
}
}
public static void main(String[] args) {
Factorial f = new Factorial();
System.out.println(f.factorial(10));
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
参考这个 计算阶乘的和
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
for循环就可以,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |