递归算法求阶乘
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
6
namespace ConsoleApplication12
7
{
8
class factorial
9
{
10
public int Factorial(int n)
11
{
12
if(n<=1)
13
{
14
return 1;
15
}//end if
16
else
17
{
18
return n*Factorial(n-1);
19
}//end else
20
}//end Factorial()
21
}
22
class Program
23
{
24
static void Main(string[] args)
25
{
26
int n=0;
27
int result=0;
28
Console.Write("输入要计算阶乘的数字:");
29
n = Int32.Parse(Console.ReadLine());
30
31
factorial fa = new factorial();
32
result = fa.Factorial(n);
33
Console.WriteLine("{0}!={1}",n,result);
34
Console.ReadLine();
35
}//end Main()
36
}//end class Program
37
}//end namespace
38
using System;2
using System.Collections.Generic;3
using System.Linq;4
using System.Text;5

6
namespace ConsoleApplication127
{8
class factorial9
{10
public int Factorial(int n)11
{ 12
if(n<=1)13
{14
return 1; 15
}//end if16
else17
{18
return n*Factorial(n-1);19
}//end else 20
}//end Factorial()21
}22
class Program23
{24
static void Main(string[] args)25
{26
int n=0;27
int result=0;28
Console.Write("输入要计算阶乘的数字:");29
n = Int32.Parse(Console.ReadLine());30
31
factorial fa = new factorial();32
result = fa.Factorial(n);33
Console.WriteLine("{0}!={1}",n,result);34
Console.ReadLine();35
}//end Main()36
}//end class Program37
}//end namespace38




浙公网安备 33010602011771号