Write a complete C++ program that prompts the user to enter a value for n, the number of terms of the series to be summed up. Your program then repeatedly computes the value for each term and accumulates it in an accumulator. After values for all n terms have been computed and accumulated, the program displays the total accumulated values as the approximation for π in a format similar to the sample output shown below. Caution: Don’t forget that the accumulated value should be multiplied by 4 as shown in the formula.
The formula is 4(-1^n)/(2n+1)
I keep getting the wrong answer, it is supposed to be close to the pi value which is 3.14.. Did I put the formula wrong ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
usingnamespace std;
int main()
{
int n, startingnumber=1;
double accumulator;
cout << "Please input a number for n. ";
cin >> n;
accumulator=0;
for (int i=startingnumber; i <= n; i++)
accumulator+=4*((-1)^n)/(2*(n)+1);
cout<<accumulator;
return 0;
}