作业帮 > 综合 > 作业

编写程序,求下面数列的前10项的和,结果保留2位小数.-sin(1!),sin(2!),-sin(3!),...,(-1

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/15 17:29:01
编写程序,求下面数列的前10项的和,结果保留2位小数.-sin(1!),sin(2!),-sin(3!),...,(-1)^n×sin(n!) (
你要用什么语言写的呢?我用c#写了一个,程序如下:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static int Factorial(int n) //求n的阶乘
        {
            if (n == 0 || n == 1)
                return 1;
            else
                return n * Factorial(n - 1);
        }
        static void Main(string[] args)
        {
            double sum = 0;
            for (int i = 1; i <= 10; i++)
            {
                sum += Math.Pow(-1, i) * Math.Sin(Program.Factorial(i));
            }
            Console.WriteLine(sum.ToString());
            Console.Read();
        }
    }
}
运行结果如图,保留两位小数的话结果是-2.27