C++ sum a sequence of positive and negative fraction
<p>I have a homework to make an algorithms for sum this sequence : <code>S = 1 - 1/2 + 1/4 - 1/6 + 1/8 - 1/10 + ... + 1/2n</code> if the user input 2 the sum will be <code>1 - 1/2 = 1/2</code> and etc. </p>

<p>Here is my coding :</p>

<pre><code>int main() {

    int num, N, k;
    double S;
    cout &lt;&lt; "enter the sequence : ";
    cin &gt;&gt; N;
    for (num = 1, k = 0, S = 1; num &lt;=N; num++) {
        num++;
        k=+2;
        if (num % 2 == 0) {
            S -= 1/k;
        } else {
            S += 1/k;
        }
    }
    cout &lt;&lt; "The sum is " &lt;&lt; S;
    system("PAUSE");
    return 0;
}
</code></pre>

<p>I'm so confused why the sum always refer to 1 ?? Can anyone explain to me why this happen??</p>
