Question #21892

calculate pi using the series

pi /4 =1-1/3+1/5-1/7+1/9- ... and so on.

output the estimate of pi after computing 100,1000,10,000 and 100,000 terms of the series.

pi /4 =1-1/3+1/5-1/7+1/9- ... and so on.

output the estimate of pi after computing 100,1000,10,000 and 100,000 terms of the series.

Expert's answer

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Calculate_pi

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine("Pi after computing 100 = " + computePi(100));

Console.WriteLine("Pi after computing 1000 = " + computePi(1000));

Console.WriteLine("Pi after computing 10000 = " + computePi(10000));

Console.WriteLine("Pi after computing 100000 = " +

computePi(100000));

Console.ReadLine();

}

private static double computePi(int n)

{

double sequenceFormula = 0;

for (int counter = 1; counter < n; counter += 2)

{

sequenceFormula = sequenceFormula + ((1.0 / (2.0 * counter - 1)) - (1.0 / (2.0

* counter + 1)));

}

double pi = 4 * sequenceFormula;

return pi;

}

}

}

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Calculate_pi

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine("Pi after computing 100 = " + computePi(100));

Console.WriteLine("Pi after computing 1000 = " + computePi(1000));

Console.WriteLine("Pi after computing 10000 = " + computePi(10000));

Console.WriteLine("Pi after computing 100000 = " +

computePi(100000));

Console.ReadLine();

}

private static double computePi(int n)

{

double sequenceFormula = 0;

for (int counter = 1; counter < n; counter += 2)

{

sequenceFormula = sequenceFormula + ((1.0 / (2.0 * counter - 1)) - (1.0 / (2.0

* counter + 1)));

}

double pi = 4 * sequenceFormula;

return pi;

}

}

}

## Comments

## Leave a comment