用c# 求阶乘?

来源:百度知道 编辑:UC知道 时间:2024/06/18 04:08:00
求1!+2!+3!......+9!+10!
要求是一个是用递归方法和非递归方法写?
希望高手写简单的代码啊?

递归方法:
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;