Answer to Question #81587 in C++ for Rohail Ahmed

Question #81587
2. The value of p can be approximated by using the following series:
-
¼ 4 1  1
3 þ
1
5  1
7 þþ
1
2n  1 þ
1
2n þ 1
 :
The following program uses this series to find the approximate value of p. However,
the statements are in the incorrect order, and there is also a bug in this program.
Rearrange the statements and also find and remove the bug so that this program can
be used to approximate p.#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double pi = 0;
long i;
long n;
cin >> n;
cout << "Enter the value of n: ";
cout << endl;
if (i % 2 == 0)
pi = pi + (1 / (2 * i + 1));
else
pi = pi - (1 / (2 * i + 1));
for (i = 0; i < n; i++)
{
pi = 0;
pi = 4 * pi;
}
cout << endl << "pi = " << pi << endl;
return 0;
1
Expert's answer
2018-10-03T05:08:48-0400
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
double pi = 0;
long i;
long n;

cout << "Enter the value of n: ";
cout << endl;
cin >> n;

pi = 0;

for(i = 0; i < n; i++)
{
if(i % 2 == 0)
pi = pi + (1.0 / (2 * i + 1));
else
pi = pi - (1.0 / (2 * i + 1));
}

pi = 4 * pi;

cout << endl << "pi = " << pi << endl;

return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS