Question

The value of π can be approximated by using the following series: The program in main.cpp...

The value of π can be approximated by using the following series: The program in main.cpp uses this series to find the approximate value of π. However, the statements are in the incorrect order, and there is also a bug in this program. Rearrange the statements and remove the bug so that this program can be used to approximate π.

The code that they have:

#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;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

#include <iostream>

#include <iomanip>

using namespace std;

int main()

{

double pi = 0;

long i;

long n;

  


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

cout << endl;

  

pi = 0;


for (i = 0; i < n; i++)

{

if (i % 2 == 0)

pi = pi + (1.0 / (2.0 * (double)i + 1.0));

else

pi = pi - (1.0 / (2.0 * (double)i + 1.0));

}
pi = 4.0 * pi;

  

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

return 0;

}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
The value of π can be approximated by using the following series: The program in main.cpp...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT