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

2024-11-12 08:08:26
推荐回答(5个)
回答(1):

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();

}

}

}


回答(2):

public int jc(int n) {
if(n==1)
return 1;
else
return n*jc(n-1);
}

回答(3):

private double jc(int n)
{
double jc1 = 1;
for (int i = 1; i <= n;i++ )
{
jc1 = jc1 * i;
}
return jc1;
}

回答(4):

public static int jiecheng(int n)
{
if (n == 1)
return 1;
else
return n * jiecheng(n - 1);
}

回答(5):

你好
public int GetResult(int n)
{
switch(n)
{
case 0:
case 1:
return 1;
default :
return n*GetResult(n-1);
}
}