using System; class EstimatePI { static double GetPI(int num_terms) { double denom; double fourth_pi = 0.0; int sign = 1; //PI/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + .... for(int i=1; i <= num_terms; i++) { denom = 2*i-1; fourth_pi += sign*(1.0/denom); sign = -sign; } return 4.0*fourth_pi; } public static void Main() { Console.WriteLine("Estimate of PI with {0} terms is {1}\n", 100, GetPI(100)); Console.WriteLine("Estimate of PI with {0} terms is {1}\n", 1000, GetPI(1000)); Console.WriteLine("Estimate of PI with {0} terms is {1}\n", 1000000, GetPI(1000000)); Console.WriteLine("Math.PI = {0}. ", Math.PI); } }