C++ Pi Approximation using Leibniz Formula
<p>I'm a beginner at C++ and coding itself, so please forgive any vocabulary mishaps. I couldn't find this specific question but similar ones on the internet, but I'm still having a hard time getting an outcome I need.</p>

<p>So I'm using the Leibniz Formula to approximate pi which is: </p>

<p>pi = 4 · [ 1 – 1/3 + 1/5 – 1/7 + 1/9 … + (–1 ^ n)/(2n + 1) ]. </p>

<p>I've written a <a href="https://ideone.com/en2U9d" rel="nofollow">compilable and runnable program</a> , but the main part of the code that's troubling me is:</p>

<pre><code>if (terms &gt; 0){

        double partial = 0;

        for (i = 0; i &lt; terms; i++)
            if (i % 2 == 0)
                partial -= (pow(-1,terms))/((2.0 * i) + 1);
            else
                partial += (pow(-1,terms))/((2.0 * i) + 1);

        double newPi = 4 * partial;

        cout &lt;&lt; "The approximation is " &lt;&lt; newPi &lt;&lt; " using " &lt;&lt; terms &lt;&lt; " terms.\n";

}
</code></pre>

<p>If terms = 3, the approximation = 2.895</p>

<p>If terms = 10, the approximation = 3.232</p>

<p>If terms = 50, the approximation = 3.161</p>

<p>I'm not getting any of these numbers. I've noticed though, that when I put in an odd number, I get a negative and vice versa with even numbers. Everything I have written in my program is everything I have learned in my class so far, so I cannot really go out of the scope I've written in. Any help or explanation would be appreciated.</p>