Okay so I'm kinda stumped... here are our directions.. My code is at the bottom of this post
"Write a program consisting of only the main function, called piApproximator.cpp. When your program begins, the user is prompted to enter a number n representing the number of terms to be used in the approximation. Your
program should then compute the series approximation of π using the first n terms of the series described above and display that approximation."
The series is π = Summation: (-1)^(i+1)*[4/(2i-1)] = 4 [1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11....]
Sample Run 3:
This program approximates pi using an n-term series expansion.
Enter the value of n> 6
pi[6] = 4[1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11] = 2.97604617604617560644
Sample Run 4:
This program approximates pi using an n-term series expansion.
Enter the value of n> 10
pi[10] = 4[1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 - 1/15 + 1/17 - 1/19] = 3.04183961892940146754
okay so that is what the output is supposed to look like..
here is my code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main ()
{
double pi = 0;
double i;
int n;
cout<<"This program approximates pi using an n-term series expansion."<<endl;
cout<<"Enter the value of n>";
cin>>i;
{
if (n<=0)
cout<<" "<<n<<" is a invalid number of terms."<<endl;
}
for (long int n = 1; n <= i; n++)
{
pi += (double) pow(-1, n+1)*(4/(2*n-1));
}
pi *= 4;
cout<<"pi"<<"["<<n<<"]"<<" = "<<pi;
return 0;
}