Question

Use your class to calculate rational approximations for π using the series π = 4 /1...

Use your class to calculate rational approximations for π using the series π = 4 /1 − 4/ 3 + 4/ 5 − 4 /7 + . . . . Your program should ask the user how many terms to include in the approximation. For full credit, use the class above as much as possible (to do all the calculations).

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class PiCalculation {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("How many terms do you want? ");
        int n = in.nextInt();
        double pi = 0;
        int sign = 1;
        for(int i = 0; i < n; ++i) {
            pi += (4 * sign) / (2*i + 1.0);
            sign = -sign;
        }
        System.out.println("Calculated value of pi is " + pi);
    }

}

Add a comment
Know the answer?
Add Answer to:
Use your class to calculate rational approximations for π using the series π = 4 /1...
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
  • 7. Use your class to calculate rational approximations for π using the series π=1-1+1-1+, . Your program should ask the user how many terms to include in the approximation. For full credit, use t...

    7. Use your class to calculate rational approximations for π using the series π=1-1+1-1+, . Your program should ask the user how many terms to include in the approximation. For full credit, use the class above as much as possible (to do all the calculations) 7. Use your class to calculate rational approximations for π using the series π=1-1+1-1+, . Your program should ask the user how many terms to include in the approximation. For full credit, use the class...

  • Write a program in Python that approximates the value of π by summing the terms of...

    Write a program in Python that approximates the value of π by summing the terms of this series: 4/1-4/3 + 4/5- 4/7 + 4/9- 4/11 + ... The program should prompt the user for n, the number of terms to sum, and then output the sum of the first n terms of this series. Have your program subtract the approximation from the value of math. pi to see how accurate it is.

  • Make a new class called Rational to represent rational numbers in C++ language. Use a long...

    Make a new class called Rational to represent rational numbers in C++ language. Use a long to store each part (numerator and denominator). Be sure your main function fully test the class. 1. Create a helper function called gcd to calculate the greatest common divisor for the two given parameters. See Euclid’s algorithm (use google.com). Use this function to always store rationals in “lowest terms”. 2. Create a default, one argument and two argument constructor. For the two argument constructor,...

  • Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int...

    Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int = 0, int = 1 ); // default constructor Rational addition( const Rational & ) const; // function addition Rational subtraction( const Rational & ) const; // function subtraction Rational multiplication( const Rational & ) const; // function multi. Rational division( const Rational & ) const; // function division void printRational () const; // print rational format void printRationalAsDouble() const; // print rational as...

  • There are also approximations of higher order derivatives that can be computed using only values ...

    Number 9 requires number 8 so please can you answer both? Thanks. Here's more context: There are also approximations of higher order derivatives that can be computed using only values of the original function. Consider the approximation: u(a + 2h)-2u(a + h) + u (a) h2 8. Using your knowledge of Taylor series, what derivative is approximated by Equa Many different combinations of terms can be used to create approximations to deriva- tion??? What is the order of the approximation?...

  • and a2.4 b1 2 b3 4 bs are all zero. Find the (1 point) a) suppose you're given the following Fourier coemcients ror a function on the interval π παο al as a 5 tollowing Fourier approximations to...

    and a2.4 b1 2 b3 4 bs are all zero. Find the (1 point) a) suppose you're given the following Fourier coemcients ror a function on the interval π παο al as a 5 tollowing Fourier approximations to the Fourier series a> (an cos(n)bn sin(nx)). (z) = Fs(r) (z) and then select the letter of the graph which most closely resembles your graph. (b) Using a calculator graph the Fourier approximation (Click on a graph to enlarge it.) (c) Which...

  • C++ and/or using Xcode... Define a class of rational numbers. A rational number is a number...

    C++ and/or using Xcode... Define a class of rational numbers. A rational number is a number that can be represented as the quotient of two integers. For example, 1/2, 3/4, 64/2, and so forth are all rational numbers. (By 1/2, etc., we mean the everyday meaning of the fraction, not the integer division this expression could produce in a C++ program). Represent rational numbers as two values of type int, one for the numerator and one for the denominator. Call...

  • 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...

  • All but dont work on the julia box one which is 2 i think so 1-3-4

    all but dont work on the julia box one which is 2 i think so 1-3-4 Fitchburg State University Department of Mathematics Project #3 Math 2400: Calculus II April 11, 2019 project for Calculus II. You may work on this with up to one other fellow student. Answer all questions completely and type or neatly write out. The final project should be turned in by Tueeday, April 23. How is it that we generate For this project it helps to...

  • Write VBA functions to calculate sin (x) using the Maclaurin arcsine series, and compare the values...

    Write VBA functions to calculate sin (x) using the Maclaurin arcsine series, and compare the values for sin-1(x) from your program to those given by the Excel spreadsheet function ASIN(x). The Maclaurin arcsine expansion is given by x 3x 6 40 (2n)! sin1(x)-2((2n+1) Note: This function by definition is only defined for-1 SxS1. When you write the code for calculating it, you will need to include code that assigns a value to it that reflects it is undefined for values...

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