使用C#控制台程序和输入、输出程序,实现1-n的整数阶乘和加和。

如题
急求解!!

static void Main(string[] args)
        {
            string str = Console.ReadLine();
            int n = Convert.ToInt32(str);
            double result = sum(n);
            Console.WriteLine("阶乘的和"+result);
            Console.ReadKey();
        }
private static double sum(int n)
        {
            if (n <= 0)
            {
                return 0;
            }
            else if (n == 1)
            {
                return 1;//1的阶乘还是1. 
            }
            return sum(n - 1) + product(n);
        }
       private static double product(int n)
        {
            if (n < 0)
            {
                return 0;
            }
            else if (n == 0)
            {
                return 1;
            }
            return product(n - 1) * n;
        }

温馨提示:答案为网友推荐,仅供参考
相似回答