16

Write a program that calculate the value of π from the infinite series

 2 years ago
source link: https://www.codeproject.com/Questions/5321195/Write-a-program-that-calculate-the-value-of-from-t
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

See more:

Write a program that Calculate the value of π
from the infinite series
Print a table that shows the value of π approximated
by one term of this series, by two terms, by three
terms, and so on.

Copy Code
Output:
Accuracy set at: 40
term pi
1 4.000000
2 2.666667
3 3.466667
4 2.895238
5 3.339683
6 2.976046
7 3.283738
8 3.017072
...
...
...
40 3.116597


What I have tried:
Expand ▼   Copy Code
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace picalc

{

    class Program

    {

        static void Main(string[] args)

        {

            int accuracy;

            double piValue = 0;

            double numerator = 4.0;

            double denominator = 1.0;

            Console.Write("Enter the number of terms:");

            accuracy = Convert.ToInt32(Console.ReadLine());

            int i;

            Console.WriteLine("Term \t\tPi");

            for (i = 1; i <= accuracy; i++)

            {

                double x = (numerator / denominator);

                if ((i % 2) == 0)

                {

                    piValue -= x;

                }

                else

                {

                    piValue += x;

                }

                denominator += 2.0;

                Console.WriteLine("{0}\t\t{1:F16}", i, piValue);       

            }

            Console.ReadLine();

      }

    }

}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK