using System; using System.Collections.Generic; using System.Text; namespace 阶乘 { class program { static void Main(string[] args) { int i = 1; Console.WriteLine("请输入数字:"); int j = int.Parse(Console.ReadLine()); int x = j;//用来输出 for (; j >= 1; j--) { i = i * j; } Console.WriteLine(x + "的阶乘是 "+ i); Console.ReadLine(); } } }
有个帖子已经回了,用winform一样,输出改一下就能用了 public long Factorial(long x) { int i = 0; long Result = 1; for (i = 1; i <= x; i++) { Result = Result * i; } return Result; }public decimal ex(decimal x) { int i = 0; decimal result = default(decimal); result = 1; for (i = 1; i <= 20; i++) { result = result + Math.Pow(x, i) / Factorial(i); } return result; } protected void form1_Load(object sender, System.EventArgs e) { Response.Write(ex(1)); }
public long Factorial(long x) { if (x == 1) return x; else return Factorial(x - 1) * x; } private void txtInput_KeyPress(object sender , KeyPressEventArgs e) { if (e.KeyChar == (char)Keys.Enter) { int x = int.Parse(txtInput.Text); double result = 1; for (int i = 1 ; i <= x ; i++) { result += (double)Math.Pow(x,i) / (double)Factorial(i); } txtOutput.Text = result.ToString(); } } long类型只能算到20
using System;
using System.Collections.Generic;
using System.Text;
namespace 阶乘
{
class program
{
static void Main(string[] args)
{
int i = 1;
Console.WriteLine("请输入数字:");
int j = int.Parse(Console.ReadLine());
int x = j;//用来输出
for (; j >= 1; j--)
{
i = i * j;
}
Console.WriteLine(x + "的阶乘是 "+ i);
Console.ReadLine();
}
}
}
public long Factorial(long x)
{
int i = 0;
long Result = 1;
for (i = 1; i <= x; i++) {
Result = Result * i;
}
return Result;
}public decimal ex(decimal x)
{
int i = 0;
decimal result = default(decimal);
result = 1;
for (i = 1; i <= 20; i++) {
result = result + Math.Pow(x, i) / Factorial(i);
}
return result;
}
protected void form1_Load(object sender, System.EventArgs e)
{
Response.Write(ex(1));
}
public long Factorial(long x)
{
if (x == 1)
return x;
else
return Factorial(x - 1) * x;
}
private void txtInput_KeyPress(object sender , KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
int x = int.Parse(txtInput.Text);
double result = 1;
for (int i = 1 ; i <= x ; i++)
{
result += (double)Math.Pow(x,i) / (double)Factorial(i);
}
txtOutput.Text = result.ToString();
}
}
long类型只能算到20