Results 1 to 3 of 3
  1. #1
    kissmeeq is offline Member
    Join Date
    Aug 2011
    Posts
    8
    Rep Power
    0

    Default can someone help me to figure this out >.<

    IT 112/ ICS 112 Midterm Laboratory Exercise Set 01

    Problem 1: (GregorySeriesLimit.java)
    The number “pi” (3.14159...) can not be expressed as a simple ratio of two numbers. Instead, the value of pi is typically calculated by summing up the terms of an infinite number series. As more and more terms in the series are evaluated, the sum approaches the “true” value of pi. One series that can be used for this purpose is called the Gregory series, which computes the value of pi as follows:
    pi = 4/1 – 4/3 + 4/5 – 4/7 + 4/9 – 4/11 + …
    Observe that the numeric values of the terms of the series get smaller and smaller as the calculation progresses. For example, the value of the 1st term is 4/1 = 4, the 2nd term is 4/3 = 1.333…, the 3rd term is 4/5 = 0.8, and so on.
    Write a program that calculates the value of pi using the Gregory series. The input to the program will be a decimal value called limit. The program shall proceed to calculate the value of pi by summing up the terms of the series, but only until such time that the value of the term being summed becomes less than or equal to the value of limit, at which point the program terminates the summation. Thus, the last term of the series that is added to the sum is the first term whose value is less than or equal to the value of limit.
    The program then prints out the calculated value of pi at that point, as well asthe actual number of terms that were summed up by the calculation.



    SAMPLE OUTPUT:

    1.) Input Limit: 0.005

    Calculated Value of PI: 3.1659792728432157
    No. of Terms Summed: 41

    2.)Input Limit: 0.00001

    Calculated Value of PI: 3.141597653564762
    No. of Terms Summed: 200001

    3.)Input Limit: 0.075

    Calculated Value of PI: 3.1058897382719475
    No. of Terms Summed: 28

  2. #2
    kissmeeq is offline Member
    Join Date
    Aug 2011
    Posts
    8
    Rep Power
    0

    Default

    AND THIS IS MY PROGRAM BUT I DONT KNOW WHAT IS WRONG >.<



    /* this program calculates PI using the Gregory series...
    */
    import java.lang.*;

    public class GregorySeries {

    public static void main(String[] args) {
    // get user input...
    System.out.print("Number of terms to sum up: ");


    int n=1;

    // ensure that the input value is valid...
    if (n < 1) {
    System.out.println("Please input a positive integer value.");
    System.exit(0);
    }

    // initialize variables...
    double pi = 0; // 'running' sum...
    double divisor = 1; // divisor for the current term
    boolean add = true; // flag whether to add or subtract
    // term from the running sum...
    int term = 1; // number of terms summed so far...

    while (term <= n) {
    // add or subtract the current term to or from the
    // running sum, as appropriate...
    if (add) {
    pi += 4 / divisor;
    } else {
    pi -= 4 / divisor;
    }

    // update variables...
    divisor += 2;
    add = !add;
    term++;
    }

    // output calculated value of PI...
    System.out.println("\nValue of PI: " + pi);
    }
    }

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by kissmeeq View Post
    AND THIS IS MY PROGRAM BUT I DONT KNOW WHAT IS WRONG >.<


    Java Code:
            System.out.print("Number of terms to sum up: ");
    You're not supposed to sum up a fixed number of terms; you are supposed to keep summing up until the last term < limit (reread your assignment text again).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. can someone please help me to figure this out >.<
    By lyzeofkiel in forum New To Java
    Replies: 11
    Last Post: 07-24-2011, 11:34 AM
  2. Can't figure out
    By beauti477 in forum New To Java
    Replies: 3
    Last Post: 07-20-2011, 03:58 PM
  3. Cant figure this out
    By Shimless12 in forum New To Java
    Replies: 1
    Last Post: 07-10-2011, 09:48 PM
  4. Need help - I can't figure it out.
    By Joshsmith in forum New To Java
    Replies: 2
    Last Post: 10-23-2009, 10:12 PM
  5. I can't figure this out
    By silvia in forum New To Java
    Replies: 3
    Last Post: 07-20-2007, 04:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •