Question

Write a c++ program to calculate the approximate value of pi using this series. The program...

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++

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

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

Enter number of terms: 50 value of pi forn50 is 3.18157668544 Do you want to try again(y or n)? y Enter number of terms 100 v

Add a comment
Know the answer?
Add Answer to:
Write a c++ program to calculate the approximate value of pi using this series. The program...
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
  • Write a Java program which implements both the while and for loop, as needed, to draw...

    Write a Java program which implements both the while and for loop, as needed, to draw the illustrations given below. Shape 1 123456789 12345678 1234567 123456 12345 1234 123 12 1 Shape 2 ************* ************* ************* ************* ************* Shape 3 ============ * * * * * * * * * * ============ Hint: To draw the shapes above, you can use nested for loops to accomplish the task. The outer for loop iterates on each row, and the inner for...

  • Write a program that produces the following output for a random number of digits on a...

    Write a program that produces the following output for a random number of digits on a row and a random number of rows. 1****** 12***** 123**** 1234*** 12345** 123456* 1234567 Write a program that calculates the following series: i=1n(12)n=12+14+18+...+12n (hint: we need to talk about integer division) Use for loops to write a code segment that prints the following output. 1 2 3 4 2 4 6 8 3 6 9 12 4 8 12 16 5 10 15 20

  • Write a C++ program using a while loop to calculate the value of pi/2 accurate to...

    Write a C++ program using a while loop to calculate the value of pi/2 accurate to 6 digits after the decimal point using the following series: DIsplay the following and turn in printouts of the program and the results: The number of terms needed to find pi/2 THe value of pi/2 using acos(-1.0)/2 using 10 digits after the deciaml point The value of pi/2 found with the series using 10 digits after the decimal points (The first 6 digits after...

  • What to do Write a C program that computes Pi with the approximation algorithm that I...

    What to do Write a C program that computes Pi with the approximation algorithm that I introduced in class. I want you to write the program in two different ways: The first version will add up the first 28284277 elements of the approximation series in a simple for loop. I ask you to put the code into a function with this signature: float get_pi_for(void); The second version will break out of a while loop depending on whether a pair of...

  • this is a C++ program! You will write a program that performs the following tasks for...

    this is a C++ program! You will write a program that performs the following tasks for a simple mathematical calculator: (1) Open a user-specified file for input. Prompt for the name of the file, read it into a string variable, echo print it to the terminal and then open it. If the file is not opened, enter into a loop that prints out an error message, resets the input file stream variable (see the hints section), obtains a new file...

  • Student ID: 123 Write a C+ program with the following specifications: a. Define a C++ function (name it function_Student...

    Student ID: 123 Write a C+ program with the following specifications: a. Define a C++ function (name it function_StudentlD where StudentID is your actual student ID number) that has one integer input (N) and one double input (x) and returns a double output S, where N S = n 0 and X2 is given by 0 xeVn n 0,1 Хл —{2. nx 2 n 2 2 m2 x2 3 (Note: in the actual quiz, do not expect a always, practice...

  • Write a program that approximates the sum of this geometric series using a user- specified number...

    Write a program that approximates the sum of this geometric series using a user- specified number of terms. For example, if you named your program approx, calling approx(3) should use the first 3 terms of the series to calculate the approximation: Approximate result = 1/2 + 1/4 + 1/8 = 0.875... Note: the values are all powers of 1⁄2: (1/2)1, (1/2)2, (1/2)3, ... Next, have your program calculate the difference (rounded to 3 decimal places) between the value “1” and...

  • The natural log of 2, (In 2) can be found using the power series below: In...

    The natural log of 2, (In 2) can be found using the power series below: In 2 1- Create an M-file function to implement this formula so that it computes and displays the values of In 2 as each term in the series is added. In other words, compute and display in sequence the values for up to the order term, n, as chosen by the user of the function. For each of the preceding, compute and display the percent...

  • Write a program in C: Using atoi() function to calculate the average of an input series...

    Write a program in C: Using atoi() function to calculate the average of an input series of number. User enter q or Q it will exit program and return average of series of number above. Example output: Enter the first number or Q to quit: 5 Enter the next number or Q to quit: 8 Enter the next number or Q to quit: -1 Enter the next number or Q to quit: 58 Enter the next number or Q to...

  • Write a Java program that uses the Monte Carlo method to estimate the value of PI....

    Write a Java program that uses the Monte Carlo method to estimate the value of PI. This method uses the unit circle inscribed in a square with sides of length 2 and random numbers to perform the estimation. The estimation works as follows: • Two random numbers are generated during each iteration of a loop. • The random numbers are each in the range of -1 to 1. One random number is the x-coordinate and the other is the y-coordinate....

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