Write a c++ program to calculate the approximate value of pi using this series. The program takes an input n that determines the number of values we are going to use in this series. Then output the approximation of the value of pi. The more values in the series, the more accurate the data. Note 5 terms isn’t nearly enough. You will try it with numbers read in from a file. to see the accuracy increase. Use a for loop for the calculation and a while loop that allows the user to repeat this calculation for new values n until the user says they want to end the program.


Create an input file with nano lab04bin.txt


12


123


1234


12345


123456


1234567


12345678


123456789


It has to be written in c++

Respuesta :

Answer:

#include <iostream>

#include <iomanip>

using namespace std;

int main() {

  char choice;

  cout << setprecision(12) << endl;

  while(true) {

      int sign = 1;

      double pi = 0;

      cout << "Enter number of terms: ";

      long n;

      cin >> n;

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

          pi += sign/(double)i;

          sign = -sign;

      }

      pi *= 4;

      cout << "value of pi for n = " << n << " is " << pi << endl;

      cout << "Do you want to try again(y or n)? ";

      cin >> choice;

      if(choice == 'n' || choice == 'N') {

          break;

      }

  }

  return 0;

}

Explanation: