用C#编写一个求n的阶乘的函数

如题所述

class Program

{

public int Factorial(int num)

{

int result;

if (num == 1)

{

return 1;

}

else

{

result = Factorial(num - 1) * num;

}

return result;

}

class Test

{

static void Main(string[] args)

{

Console.WriteLine("请说出一个整数,我将算出他的阶乘");

int a = Convert.ToInt32(Console.ReadLine());

Program n = new Program();

Console.WriteLine("{0}的阶乘是{1}",a,n.Factorial(a));

Console.ReadKey();

}

}

}


温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-04-06
public int jc(int n) {
if(n==1)
return 1;
else
return n*jc(n-1);
}
第2个回答  2013-04-06
private double jc(int n)
{
double jc1 = 1;
for (int i = 1; i <= n;i++ )
{
jc1 = jc1 * i;
}
return jc1;
}
第3个回答  推荐于2017-10-01
public static int jiecheng(int n)
{
if (n == 1)
return 1;
else
return n * jiecheng(n - 1);
}本回答被网友采纳
第4个回答  2013-04-15
你好
public int GetResult(int n)
{
switch(n)
{
case 0:
case 1:
return 1;
default :
return n*GetResult(n-1);
}
}
相似回答