c#设计一个方法实现n的阶乘在主函数中调用并输出结果
2个回答
展开全部
如下操作:
using System;public class Factorial{
public long factorial(long x){
return ((x<=1) ? 1 : x*(factorial(x-1)));
}
}
public class FactorialSum
{
public static void Main()
{
long temp=0;
long sum=0;
Console.WriteLine ("输入一个数:");
int x=int.Parse (Console.ReadLine ());
for(int i=1;i<=x;i++)
{
Factorial f1=new Factorial ();
temp+=f1.factorial (i);
}
sum+=temp;
Console.WriteLine ("1!+2!+3!+...+a!="+sum);
}}
using System;public class Factorial{
public long factorial(long x){
return ((x<=1) ? 1 : x*(factorial(x-1)));
}
}
public class FactorialSum
{
public static void Main()
{
long temp=0;
long sum=0;
Console.WriteLine ("输入一个数:");
int x=int.Parse (Console.ReadLine ());
for(int i=1;i<=x;i++)
{
Factorial f1=new Factorial ();
temp+=f1.factorial (i);
}
sum+=temp;
Console.WriteLine ("1!+2!+3!+...+a!="+sum);
}}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
using System;public class Factorial{
public long factorial(long x){
return ((x<=1) ? 1 : x*(factorial(x-1)));
}
}
public class FactorialSum
{
public static void Main()
{
long temp=0;
long sum=0;
Console.WriteLine ("输入一个数:");
int x=int.Parse (Console.ReadLine ());
for(int i=1;i<=x;i++)
{
Factorial f1=new Factorial ();
temp+=f1.factorial (i);
}
sum+=temp;
Console.WriteLine ("1!+2!+3!+...+a!="+sum);
}}
public long factorial(long x){
return ((x<=1) ? 1 : x*(factorial(x-1)));
}
}
public class FactorialSum
{
public static void Main()
{
long temp=0;
long sum=0;
Console.WriteLine ("输入一个数:");
int x=int.Parse (Console.ReadLine ());
for(int i=1;i<=x;i++)
{
Factorial f1=new Factorial ();
temp+=f1.factorial (i);
}
sum+=temp;
Console.WriteLine ("1!+2!+3!+...+a!="+sum);
}}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询