递归算法求阶乘
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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38
