helpp I need c++ coded plzz

I want to write a program to calculate Pi:
Pi=4-4/3+4/5-4/7+4/9-4/11+...
(the user inputs the number of terms in the series)
Is this right ?
#include<iostream>
using namespace std;
int main ()
{
double n=4.0;
double d=1.0;
double pi;
cout<<"Enter the number of terms in the series";
cin>>n>>d;
while(d<=10)
{
d=d+2;
n=-n;
pi=n/d;
cout<<"Pi="<<pi<<endl;
}
system("pause");
return 0;
}
How to post your code
http://www.cplusplus.com/forum/beginner/95141/


Hey, it is easy as pie. :)
Just a few minutes googling.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Leibniz's Formula for Pi
// pi = 4 * SUM{k>=0}  { [ (-1)^k ] * [ 1 / ( 2*k + 1 ) ] }
// Pi = 4/1 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + ...
// http://www.proofwiki.org/wiki/Leibniz%27s_Formula_for_Pi

#include<iostream>
#include<cmath>     // this is for pow()
using namespace std;
int main ()
{
    double numofTerms;
    double pi=0;

    cout<<"Enter the number of terms in the series: ";
    cin>>numofTerms;

    for (double k=0.0; k<=numofTerms; k++) {
        pi += 4.0 * ( pow((-1.0),k) ) * ( 1.0 / (2.0*k + 1) );
        cout<<"k= "<<k<<" ==> pi= "<<pi<<endl;
    }

    //system("pause");
    return 0;
}
Last edited on
Topic archived. No new replies allowed.