Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: help w/ PI Math java program

  1. #1
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default help w/ PI Math java program

    im currently in entry level java class and need help with a code..here are my instructions

    Mathematicians define the constant PI to be
    PI = 4 *( 1 - 1/(2*2-1) + 1/(2*2+1) - 1/(2*4-1) + 1/(2*4+1) - 1/(2*6-1) + 1/(2*6+1)- . . . .format . . . . . - 1/(2*i-1) + 1/(2*i+1) )
    Note that i starts at 2 and is incremented by 2. The higher the i, the more precise PI will be. Also note that the higher the value of i, the smaller the value of PI.

    Write a program that displays the value of PI for i = 100, 200, 300, . . ,1000. Output should be something like this :
    when i is 100 PI is xxxx
    when i is 200 PI is xxxx
    etc . .
    when i is 900 PI is xxxx
    when i is 1000 PI is xxxx

    Once you have this working, enhance the program, add another section. The next section of the program should determine the value of i needed to achieve a value of PI that is equal to or slightly smaller than 3.1419. The program should output the following message :
    value of i to give PI equal or smaller than 3.1419 is xxxxxx


    and here is my code so far, i underlined the part where im getting an errors
    should i put each value of PI in an if statement?? im kinda stuck right now thanks

    public class ConstantPI {
    public static void main(String[] args)
    {
     
     
    [U]int i = 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000;[/U]
    double pi4 = 1.0;
    for(int j = 2; j <= i; j += 2)
    pi4 = pi4 - 1/(2*(double)j - 1) + 1/(2*(double)j + 1);
    double pi = 4.0*pi4;
    System.out.println("When i is " + i + " PI is " + pi);
    }
     
    }


    btw crossposted thanks

    help w/ PI Math java program - Java Forums
    Last edited by robertsbd; February 15th, 2011 at 09:44 PM.


  2. #2
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: help w/ PI Math java program

    alright here is my updated code but still not sure how to calculate the pi when i =200,300, etc

    also i tried to work on the 2nd part of my code

    public class Assign3_Roberts {
    public static void main(String[] args)
    {
     
     
    int i = 100;
    double pi4 = 1.0;
    for(int j = 2; j <= i; j += 2)
    pi4 = pi4 - 1/(2*(double)j - 1) + 1/(2*(double)j + 1);
    double pi = 4.0*pi4;
    System.out.println("When i is " + i + " PI is " + pi);
    }
     
     
     
    //next section
    //value of i
    int i = 0;
    double pi = 100;
    {
    do
    {
    i ++;
    double pi4 = 1.0;
    for(int j = 2; j <= i; j += 2)
    pi4 = pi4 - 1/(2*(double)j - 1) + 1/(2*(double)j + 1);
    pi = 4.0*pi4;
    } while (pi > 3.1419);
    System.out.println("value of i to give PI equal or smaller than 3.1419 is " + i);
    }
    }

    and when i run the program my output is:

    When i is 100 PI is 3.1514934010709914

  3. #3
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: help w/ PI Math java program

    alright i updated my code w/ a for loop and believe im getting some where

    public class Assign3_Roberts {
    public static void main(String[] args)
    {
    for (int i = 100; i <= 1000; i += 100) {
    	double pi4 = 1.0;
    	for(int j = 2; j <= i; j += 2)
    	pi4 = pi4 - 1/(2*(double)j - 1) + 1/(2*(double)j + 1);
    	double pi = 4.0*pi4;
    	System.out.println("When i is " + i + " PI is " + pi);
    	}
    	}
     
    //next section
    //determine value of i
     
    int i = 0;
    double pi = 100;
    {
    do
    {
    i ++;
    double pi4 = 1.0;
    for(int j = 2; j <= i; j += 2)
    pi4 = pi4 - 1/(2*(double)j - 1) + 1/(2*(double)j + 1);
    pi = 4.0*pi4;
    } while (pi > 3.1419);
    System.out.println("value of i to give PI equal or smaller than 3.1419 is " + i);
    }
    }

    and output is now:
    When i is 100 PI is 3.1514934010709914
    When i is 200 PI is 3.1465677471829556
    When i is 300 PI is 3.1449149035588526
    When i is 400 PI is 3.144086415298761
    When i is 500 PI is 3.143588659585789
    When i is 600 PI is 3.143256545948974
    When i is 700 PI is 3.1430191863875865
    When i is 800 PI is 3.142841092554028
    When i is 900 PI is 3.1427025311614294
    When i is 1000 PI is 3.1425916543395442

    but not getting the 2nd section right

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: help w/ PI Math java program

    Should work it seems.

  5. #5
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: help w/ PI Math java program

    but it never showed the output of what the value of I is for PI to = 3.1419 or lower

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: help w/ PI Math java program

    Quote Originally Posted by robertsbd View Post
    but it never showed the output of what the value of I is for PI to = 3.1419 or lower
    Sorry. I was looking at a glance then. I had to be somewhere right after I wrote that post.

    BTW, what are you getting for the second section?

  7. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: help w/ PI Math java program

    Hmmmmmm....I think I mighta found the problem.

    You had some extra brackets that you didn't need.
    public class Assign3_Roberts {
    public static void main(String[] args)
    {
    for (int i = 100; i <= 1000; i += 100) {
        double pi4 = 1.0;
        for(int j = 2; j <= i; j += 2)
        pi4 = pi4 - 1/(2*(double)j - 1) + 1/(2*(double)j + 1);
        double pi = 4.0*pi4;
        System.out.println("When i is " + i + " PI is " + pi);
     
        }
     
    //next section
    //determine value of i
     
    int i = 0;
    double pi = 100;
     
    do
    {
    i ++;
    double pi4 = 1.0;
    for(int j = 2; j <= i; j += 2)
    pi4 = pi4 - 1/(2*(double)j - 1) + 1/(2*(double)j + 1);
    pi = 4.0*pi4;
    } while (pi > 3.1419);
    System.out.println("value of i to give PI equal or smaller than 3.1419 is " + i);
    }
    }

     



    ----jGRASP: operation complete.

    ----jGRASP exec: java Assign3_Roberts

    When i is 100 PI is 3.1514934010709914
    When i is 200 PI is 3.1465677471829556
    When i is 300 PI is 3.1449149035588526
    When i is 400 PI is 3.144086415298761
    When i is 500 PI is 3.143588659585789
    When i is 600 PI is 3.143256545948974
    When i is 700 PI is 3.1430191863875865
    When i is 800 PI is 3.142841092554028
    When i is 900 PI is 3.1427025311614294
    When i is 1000 PI is 3.1425916543395442
    value of i to give PI equal or smaller than 3.1419 is 3254


    Last edited by javapenguin; February 16th, 2011 at 08:14 PM.

  8. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: help w/ PI Math java program

    I'm still baffled as to how that lower for loop even works since it appears that i is less than j from the beginning.



    Oh well. It is valid. I checked it.
    Last edited by javapenguin; February 16th, 2011 at 08:32 PM.

  9. #9
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: help w/ PI Math java program

    ah..good shot man thanks

Similar Threads

  1. My program doesnt want to do its math point my errors out please
    By Redlight in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 31st, 2010, 01:56 PM
  2. Help need on math java program
    By zidangus in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 6th, 2010, 07:41 PM
  3. i can run my program but the math doesnt come otu
    By pairenoid in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 7th, 2010, 01:39 PM
  4. math quiz program
    By hope.knykcah in forum Collections and Generics
    Replies: 1
    Last Post: October 23rd, 2009, 09:53 AM
  5. Math in Java?
    By [Kyle] in forum Java Theory & Questions
    Replies: 3
    Last Post: September 23rd, 2009, 12:21 PM