Jon Butler - 1 month ago 9x

C# Question

http://imageshack.com/a/img923/5162/R6tZQr.jpg

is the outcome the is supposed to occur. here is my code

`static void CalcN()`

{

Console.Write(" ");

for (uint n = 1; n <= 9; n++)

{

p = (n*n*(sides-2) - n*(sides-4)) / 2;

Console.Write("{0,3} ", p);

sumOfRecip += (1/p);

}

Console.WriteLine("{0}",sumOfRecip);

}

and my output showing the calculations for "n" are correct, but when trying to add sum of reciprocals, it is wrong. and I can't get Math.Round to work with the calculating reciprocals.

`Sum of`

n=9 recip

--- ------

45 1.8000

81 3.3398

117 4.7504

153 6.0827

189 7.3620

225 8.6031

261 9.8152

297 11.0046

333 12.1756

369 13.3316

405 14.4750

Answer

You probably need to reset your `sumOfRecip`

so it just doesnt keep getting added to.

```
3.3398 - 1.8 = the value you want.
```

You should make `sumOfRecip`

a local variable inside `CalcN()`

unless you really need it outside that function. Make variables as local as they can be.

Source (Stackoverflow)

Comments