Question

In: Computer Science

Replace <missing code> with your answer. Your code should approximate the value of π (~3.141592653589793). You...

Replace <missing code> with your answer. Your code should approximate the value of π (~3.141592653589793). You can approximate pi using the following formula:

π = 4 * (1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 - 1/15 + …)

For this problem, let n be the number of terms to add in the series. For example, if n = 5, your code should compute the following: 4 * (1 - 1/3 + 1/5 - 1/7 + 1/9). If n = 3, your code should compute 4 * (1 - 1/3 + 1/5)

public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();

int piApprox = <missing value>;

<missing code>

System.out.println("piApprox = " + piApprox);

}

Solutions

Expert Solution

import java.util.Scanner;

public class CalcPiApprox {

   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       int n = scanner.nextInt();

       // we need to make piApprox double
       // otherwise, values will be rounded off
       double piApprox = 0;

       // Initially, set denominator for term to 1
       double denom = 1;
       int terms = 0;

       while (terms < n) {
           // Increment denom by 2 and term by 1 after calculating one term
           piApprox = piApprox + 1 / denom;
           denom += 2;
           terms += 1;

           // Check if term = n, if yes break
           // This could be the case when n is odd
           if (terms == n) {
               break;
           }
           // Increment denom by 2 and term by 1 after calculating another term
           piApprox = piApprox - 1 / denom;
           denom += 2;
           terms += 1;
       }

       // Multiply piApprox by 4 and print the result
       piApprox = piApprox * 4;
       System.out.println("piApprox = " + piApprox);
   }
}

OUTPUT


Related Solutions

Replace <missing value> and <missing code> with your answers. Your code should compute the factorial of...
Replace <missing value> and <missing code> with your answers. Your code should compute the factorial of n. For example, if n = 5, your code should compute the following 5 * 4 * 3 * 2 * 1 (which is 120). public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int factorial = <missing value>; <missing code> System.out.println("factorial = " + factorial);
Answer the following questions in order to approximate the value of sin(0.8). Note that π/ 4...
Answer the following questions in order to approximate the value of sin(0.8). Note that π/ 4 ≈ 0.785 radians. (a) Do you have enough information to use right triangles to estimate sin(0.8)? Why? (b) Estimate sin(0.8) using values of sin(x) that you know from part (2). (c) Estimate sin(0.8) using the graph of y = sin(x) from part (3). (d) Estimate sin(0.8) using the 5th degree Taylor polynomial for sin(x) at a = 0 (I will accept either the Taylor...
Use differentials to approximate the value of the expression. Compare your answer with that of a...
Use differentials to approximate the value of the expression. Compare your answer with that of a calculator. (Round your answers to four decimal places.) 3 26
Solve the initial value problem: 9y″+18y′+19y=0, y(π/2)=−2, y′(π/2)=−2. Give your answer as y=... . Use x...
Solve the initial value problem: 9y″+18y′+19y=0, y(π/2)=−2, y′(π/2)=−2. Give your answer as y=... . Use x as the independent variable.
Please answer the following: 1. Should you develop your own personal value proposition? Explain 2. What...
Please answer the following: 1. Should you develop your own personal value proposition? Explain 2. What is an SBU? 3. What does SWOT stand for and how do companies use a SWOT analysis. 4. Explain the BCG matrix
A column in your data is missing a value in one row. Which command fills this...
A column in your data is missing a value in one row. Which command fills this missing data point with the value before it? Select an answer: df.fillna(method='bfill') df.fillna(0) df.fillna(method='linear') df.fillna(method='ffill')
What is my code missing? // chStr.cpp // Put your name here // Pseudocode #include #include...
What is my code missing? // chStr.cpp // Put your name here // Pseudocode #include #include <___> #include using namespace std; const string ANAME = "Katia Kool"; const char AGRADE = 'A'; const double COMMISSION_RATE_A = 0.02625; const ___; . . . // likewise, constants for Betty int main() {        int numberOfShares = 622;                // number of shares        double pricePerShareA = 21.77;           // price per share        double pricePerShareB = 12.44;           // price per share        double stockA,...
Calculate an approximate value of the area of the place inside the curve. (So if you...
Calculate an approximate value of the area of the place inside the curve. (So if you sketch it up (by using WolframAlpha or something else) then you will see that what is sought after is the calculated value of the area that is inside the curve 1 = (40+43)x^2y^2 + y^4 + (1+1)x^4
can you please explain each and every statement of the code without missing. it would be...
can you please explain each and every statement of the code without missing. it would be really helpful AVA PROJECT CREATING GUI WITH ARRAY Read from a file that contains a paragraph of words. Put all the words in an array, put the valid words (words that have only letters) in a second array, and put the invalid words in a third array. Sort the array of valid words using Selection Sort. Create a GUI to display the arrays using...
Additional Requirements 1.The name of your source code fileshould be ProbEst.py All your code should be...
Additional Requirements 1.The name of your source code fileshould be ProbEst.py All your code should be within a single file. 2.You cannot import any package except for pandas.You need to use the pandas DataFrame object for storing data Requirements Y ou are to create a program in Python that performs the following: 1. Asks the user for the number of cars (i.e. data instances) . 2. F or each car , ask s the user to enter the following fields:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT