help verifying code

Write a program that computes the value of the mathematical constant π, using the following series:
4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + ......
The program should ask for the numbers of terms int n, then compute double pi using the first n terms. I am unsure if I have set the code so that it is equivalent to the denominators of the series written above.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <math.h>
using namespace std;

int main () {
int i, n;
cout << "Enter the number of terms: ";
cin >> n;
double pi = 0;
for (i=1; i<=n; i++)
pi += pow(-1,i+1) * 4.0 / (2*i - 1);
cout << pi;
return 0;
}


It looks good. You can try outputting the denominator's value within the loop to be sure. You could also look into using a debugger
Topic archived. No new replies allowed.