I want to calculate the sum with c++,

Sum = 1 + ... + (2n - 1) = n2

I tried like

```
double s = 0;
int i = 1;
int n = 5;
for(i = 1; i <= n; i++){
    double val = (double)i / (i + 1);
    if (i % 2 != 0){
        val *= -1.;
    }
    s += val;
}

```

but is not right correct. can someone help me?