title: Calculating Pi with Taylor Method C++ and loop
body: <p>An approximate value of pi can be calculated using the series given below:</p>
  
  <blockquote>
    <p>pi = 4 * [ 1 - 1/3 + 1/5 - 1/7 + 1/9 … + ((-1)^n)/(2n + 1) ]</p>
  </blockquote>
  
  <p>write a C++ program to calculate the approximate value of pi using this series. The program takes an input n that determines the number of terms in the approximation of the value of pi and outputs the approximation. Include a loop that allows the user to repeat this calculation for new values n until the user says she or he wants to end the program.</p>
  
  <p>The expect result is:
  Enter the number of terms to approximate (or zero to quit):
  1
  The approximation is 4.00 using 1 term.
  Enter the number of terms to approximate (or zero to quit):
  2
  The approximation is 2.67 using 2 terms.
  Enter the number of terms to approximate (or zero to quit):
  0</p>
  
  <p>I can get the correct result now but I don't know how to Include a loop that allows the user to repeat this calculation for new values n until the user says she or he wants to end the program.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  #include &lt;cmath&gt;
  using namespace std;
  
  int main()
  { int n; double pi, sum; 
  do { 
       cout &lt;&lt; "Enter the number of terms to approximate (or zero to quit):" &lt;&lt; endl;
       cin &gt;&gt; n;
  
  if (n &gt;0)
  {
  
          double sum=1.0;
          int sign=-1;
  
      for (int i=1; i &lt;=n; i++) {
      sum += sign/(2.0*i+1.0);
          sign=-sign;
      }
          pi=4.0*sum;
  
      cout.setf(ios::fixed);
      cout.setf(ios::showpoint);
      cout.precision(2);
  
      cout &lt;&lt; "The approximation is " &lt;&lt; pi &lt;&lt; " using "&lt;&lt; n &lt;&lt; " terms" &lt;&lt; endl;
  }
  } while(n&gt;0);
  
  
  
      cout &lt;&lt; endl;
  
     return 0;
  }
  </code></pre>