Problem Description
My pi is an irrational number, i.e. it cannot be written as a fraction.
It's approximate value of p is 3:141592653589793. Below are 5
dierent series which can be used to approximate p:

1. P= 4(1-(1/3)+(1/5)-(1/7)+(1/9)-...
2. P= sqrt(12(1- (1/4)+(1/9)-(1/16)+(1/25)-...))
3. P= sqrt(6 (1+ (1/4)+(1/9)+(1/16)+(1/25)+...))
4. P= sqrt(8 (1+ (1/9)+(1/25)+(1/49)+... ))
5. P= sqrt(24((1/4)+(1/16)+(1/36)+(1/64)+... ))

My project has many menus so i will post here my fuction that i have problem....
i think if this problem it fixed then i will write the other problems

so on this fuction
user must choice one of the series then give how many iterations
want's and the program will display the pi value...

My problem is that... i don't undersand what iterations means...and
how i can't calculate the value pi with???

i try wiki and google but i found differents answers, also my english it not good so
you understand that it's difficult to me to understand the problem.....

I have a code here, and i want to tell me your opinion about that..
errors,Algorithm errors , etc..........

here is my code::::

``````void menu10()
{
int  i;
double pi = 0;
char s;
system("cls");
system("color 01c");
cout << "                          ------------------------------------" << endl;
cout << "                          |              Choice 1.           |" << endl;
cout << "                          |----------------------------------|" << endl;
cout << "                          |                                  |" << endl;
cout << "                          |                                  |" << endl;
cout << "                          |Approximate Pi using one function.|" << endl;
cout << "                          |                                  |" << endl;
cout << "                          |                                  |" << endl;
cout << "                          |----------------------------------|" << endl;
cout <<""<<endl;
cout <<"                                         Series"<<endl;
cout <<"                                         ------"<<endl;
cout <<"                        1.  P= 4(1-(1/3)+(1/5)-(1/7)+(1/9)-..."<<endl;
cout <<"                        2.  P= sqrt(12(1- (1/4)+(1/9)-(1/16)+(1/25)-...))"<<endl;
cout <<"                        3.  P= sqrt(6 (1+ (1/4)+(1/9)+(1/16)+(1/25)+...))"<<endl;
cout <<"                        4.  P= sqrt(8 (1+ (1/9)+(1/25)+(1/49)+...      ))"<<endl;
cout <<"                        5.  P= sqrt(24((1/4)+(1/16)+(1/36)+(1/64)+...  ))"<<endl;
cout <<endl;
cout <<" Which series do you want? (1-5) ";
cin >> s;

if ((!s=='1')  || (!s=='2') || (!s=='3') || (!s=='4') || (!s=='5'))
{

cout<<"ERROR......."<<endl;
cout<<"Program will be terminated"<<endl;
cin>>s;
}
else
if (s=='1')
{
cout <<" On how many iterations? ";
cin >>i;

for (long int n = 1; n <= i; n++)
{
pi += (double)(-1,+n+1)/(2*n-1);
}
// Calculating pi
pi *= 4;
cout << " Estimated PI value (" << i << " iterations): is "<< pi;
cin>>pi;
}
else
if (s=='2')
{
cout <<" On how many iterations? ";
cin >>i;

for (long int n = 1; n <= i; n++)
{

pi +=  (12*(-1,+n+1)/(2*n-1));
n++;
}
pi =sqrt(pi);
cout << " Estimated PI value (" << i << " iterations): is "<< pi;
cin>>pi;
}
else
if (s=='3')
{
cout <<" On how many iterations? ";
cin >>i;

for (long int n = 1; n <= i; n++)
{
pi +=  (6*(+1,+n+1)/(2*n+1));
n++;
}
// Calculating pi
pi =sqrt(pi);
cout << " Estimated PI value (" << i << " iterations): is "<< pi;
cin>>pi;
}
else
if (s =='4')
{
cout <<" On how many iterations? ";
cin >>i;

for (long int n = 1; n <= i; n++)
{
pi +=  (8*(+1,+n+1+1)/(2*n+1+1));
n++;
}
// Calculating pi
pi =sqrt(pi);
cout << " Estimated PI value (" << i << " iterations): is "<< pi;
cin>>pi;
}
else
if (s=='5')
{
cout <<" On how many iterations? ";
cin >>i;

for (long int n = 1; n <= i; n++)
{
pi +=  (24*(+1,+n+1)/(2*n+1+1+1));
n++;
}
// Calculating pi
pi =sqrt(pi);
cout << " Estimated PI value (" << i << " iterations): is "<< pi;
cin>>pi;
}
}``````

## All 12 Replies

Iterations is the number of times to do the calculation. You use it correctly in your code:

``````cout <<" On how many iterations? ";
cin >>i;
for (long int n = 1; n <= i; n++)
{``````

so the code it's correct?? i mean, my all logic is ok???....
also i want to ask if all my mathematical it's ok...
i calculate and i have results for all , but it's give's me wrong results....

i have been confioused a little ...

Thank you very much for your help!!!!!!1

ok i fixed my problem for the one series but i confused with the others...

here is the code::::

``````void menu2();
int  i;
char s;
double pi = 0;
cout <<" Which series do you want? (1-5) ";
cin >> s;
if (s=='1')
{
cout <<" On how many iterations? ";
cin >>i;

for (long int n = 1; n <= i; n++)           //for loop for the calculationg of pi value
{
pi += (4*pow(-1.0, n+1))/(2*n - 1);

}
// Calculating pi

cout << " Estimated PI value (" << i << " iterations): is "<< pi;
}
}``````

To calculate pi it might be useful to have a high-precision float available where you can store your calculation in, apfloat is really a nice one :) ...

You have 100% right about that you say, I have heard again, if you want perfect results ------- but first i should fixed this problem.........
i cant present this series of pi .... :S iam desperd

2. P= sqrt(12(1- (1/4)+(1/9)-(1/16)+(1/25)-...))
3. P= sqrt(6 (1+ (1/4)+(1/9)+(1/16)+(1/25)+...))
4. P= sqrt(8 (1+ (1/9)+(1/25)+(1/49)+... ))
5. P= sqrt(24((1/4)+(1/16)+(1/36)+(1/64)+... ))

it's strange because i try to used if staytment inside a loop but isn't works....

something like this.....

``````for (long int n =1; n <= i; n++)
{
if (n>=2)
{
pi1 += (12*pow(-1.0, n+1))/(4*n - 4);
}
else
pi2 += (12*pow(-1.0, n+1))/(4*n - 3);
pi3=pi1+pi2;
}

cout << " Estimated PI value (" << i << " iterations): is "<< sqrt(pi3);``````

My RESULTS of this series (code) is 3.14945

Thank you very much for your interesting!!!!!!!!

>To calculate pi it might be useful to have a high-precision float available where you can store your calculation in...
To calculate pi it might be useful to have a computer with regualar IEEE floating point arithmetics (type double in C++), good algorithms and basic knowledges in computational mathematics.
;)

To calculate pi it might be useful to have a high-precision float available where you can store your calculation in, apfloat is really a nice one :) ...

That's why I mentioned it :P

tux4life

you don't undersand my question, i have install apfloat on my pc....
my big problem is other....

i don't know if my logical and mathematics calculation is correct....
in this case is my problem.....

i want someone to tell me if my code is ok or not.......

Thank you very much for your help...........

tux4life

you don't undersand my question, i have install apfloat on my pc....
my big problem is other....

i don't know if my logical and mathematics calculation is correct....
in this case is my problem.....

i want someone to tell me if my code is ok or not.......

Thank you very much for your help...........

> I actually did understand your question, but in my previous post I was talking to ArkM :) ...

> Maybe useful: http://www.pisymbol.com/apps/picalc/ (a pi calculator with C++ source)

> BTW, where did you get the mathematics to calculate pi? I'll check that out :) ...

> Edit:: This link may also be an interesting one ...

> Edit:: Also Google on: "Chudnovsky algorithm" and "Gauss-Legendre"

> I actually did understand your question, but in my previous post I was talking to ArkM :) ...

> Maybe useful: http://www.pisymbol.com/apps/picalc/ (a pi calculator with C++ source)

> BTW, where did you get the mathematics to calculate pi? I'll check that out :) ...

> Edit:: This link may also be an interesting one ...

> Edit:: Also Google on: "Chudnovsky algorithm" and "Gauss-Legendre"

I have created this code, the code that i find on web is stupid ,naturally it can be also mine stupid :P so maybe iam stupid :P:P

my question is if you can't to check this code and tell me if it's correct..........

Thank you very much!!!!!!!!!!

I have created this code, the code that i find on web is stupid ,naturally it can be also mine stupid :P so maybe iam stupid :P:P

my question is if you can't to check this code and tell me if it's correct..........

Thank you very much!!!!!!!!!!

If your code compiled correctly it's correct code for the computer, but you could always have made a 'logical mistake' in your calculation algorithm of PI, that's why I was asking you where you got the algorithm from :) ...

it's for my universidy........ this is a project that i have.... the program it's to big i have meny problems but, i don't want to post here, iam sure that i found the solution, i post the difficuld for me here.... and iam not sure if it's works in the computer professor rightly....

sorry for m bad english :) and thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.