递归方法:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(fun(6).ToString());
Console.ReadKey();
}
public static double fun(int n)
{
if (n == 1)
return 1;
else
return fun(n - 1) * n;
}
}
}
非递归方法:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(fun(6).ToString());
Console.ReadKey();
}
public static double fun(int n)
{
double S = 1;
for (int i = 0; i < n; i++)
{
S *= (i+1);
}
return S;
}
}
}
在视图界面双击【确定】进入后台代码界面,写:
static int jiecheng(int num)
{
int j = 1;
for (int i = 1; i <= num; i++)
{
j = j * i;
}
return j;
}
protected void Button1_Click(object sender, EventArgs e)
{
int num = int.Parse(TextBox1.Text.Trim());
int j = jiecheng(num);
int sum = 0;
string str = "0!";
for (int i = 1; i <= num; i++)
{
str = str + "+" + string.Format("{0}!", i);
}
for (int i = 1; i <= num; i++)
{
sum = sum + jiecheng(i);
}
this.Label1.Text = str + "=" + sum.ToString();
//Response.Write(str + "=" + sum.ToString());
}