递归算法求阶乘
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6namespace 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
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6namespace 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