C # basic arithmetic problems and find the number of columns

The number of columns and seek

Questions asked

Enter a positive integer n, seeking 1-1 / 3 + 1 / 5-1 / 7 + ...... and the total of n items.

Code

using System;

namespace _3求数列和
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = int.Parse(Console.ReadLine());
            int flag = 1;
            double sum = 0, item = 1;
            for (int i = 1; i <= num; i++)
            {
                sum += flag * (1 / item);
                item += 2;
                flag = -flag;
            }
            Console.WriteLine(sum);

            Console.ReadKey();
        }
    }
}

Guess you like

Origin www.cnblogs.com/xueyubao/p/11260670.html