Results 1 to 13 of 13
Thread: help w/ PI Math java program
 02162011, 04:28 AM #1Member
 Join Date
 Oct 2010
 Posts
 44
 Rep Power
 0
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*21) + 1/(2*2+1)  1/(2*41) + 1/(2*4+1)  1/(2*61) + 1/(2*6+1) . . . .format . . . . .  1/(2*i1) + 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, 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
Java Code: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); } }
http://www.javaprogrammingforums.com...html#post26927Last edited by clemsontigers; 02162011 at 04:31 AM.
 02162011, 04:45 AM #2Java Code:
int i = 100
 02162011, 06:36 PM #3Member
 Join Date
 Oct 2010
 Posts
 44
 Rep Power
 0
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
Java 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); } }
When i is 100 PI is 3.1514934010709914
 02162011, 06:45 PM #4Member
 Join Date
 Oct 2010
 Posts
 44
 Rep Power
 0
alright i updated my code w/ a for loop and believe im getting some where
Java Code:public class Assign3_Roberts { public static void main(String[] args) { for (int i = 100; i <= 3300; 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); } }
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 rightLast edited by clemsontigers; 02162011 at 08:18 PM.
 02162011, 07:46 PM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,039
 Blog Entries
 7
 Rep Power
 23
So the maximum value of i should go up; as you can see from your output the series slowly goes down (as your assignment text already told you). The series has a very slow convergence so you should add more terms to reach a value lower than 3.1419.
kind regards,
JosI have the stamina of a seal; I lie on the beach instead of running on it.
 02162011, 08:17 PM #6Member
 Join Date
 Oct 2010
 Posts
 44
 Rep Power
 0
ok well i changed it to 3300 and updated it in my previous post and here is my output
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
When i is 1100 PI is 3.14250091861592
When i is 1200 PI is 3.142425292912591
When i is 1300 PI is 3.1423612929843348
When i is 1400 PI is 3.1423064293732548
When i is 1500 PI is 3.142258876034187
When i is 1600 PI is 3.1422172631478604
When i is 1700 PI is 3.1421805430157748
When i is 1800 PI is 3.1421479006319486
When i is 1900 PI is 3.14211869248028
When i is 2000 PI is 3.1420924036835256
When i is 2100 PI is 3.142068617389586
When i is 2200 PI is 3.1420469925031944
When i is 2300 PI is 3.1420272472242043
When i is 2400 PI is 3.1420091466995945
When i is 2500 PI is 3.141992493637787
When i is 2600 PI is 3.141977121088081
When i is 2700 PI is 3.1419628868240537
When i is 2800 PI is 3.1419496689300788
When i is 2900 PI is 3.1419373623006863
When i is 3000 PI is 3.1419258758397897
When i is 3100 PI is 3.141915130201854
When i is 3200 PI is 3.14190505595643
When i is 3300 PI is 3.1418955920863283
 02162011, 08:27 PM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,039
 Blog Entries
 7
 Rep Power
 23
 02162011, 08:31 PM #8Member
 Join Date
 Oct 2010
 Posts
 44
 Rep Power
 0
i believe i only go up in increments of 100 b/c when i entered 3298 it only gave me the output of 1003200
 02172011, 02:29 AM #9Member
 Join Date
 Oct 2010
 Posts
 44
 Rep Power
 0
it never showed the output of what the value of I is for PI to = 3.1419 or lower
 02172011, 05:57 AM #10Java Is A Funny Language... Really!
Click on * and add to member reputation, if you find their advices/solutions effective.
 02172011, 06:17 AM #11Member
 Join Date
 Feb 2011
 Posts
 7
 Rep Power
 0
There's a mismatch in '{' and '}' to print the statement "value of i to give PI .."
public static void main(String[] args)
{
for (int i = 100; i <= 3300; 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);
}
} <  Remove this
//next section
//determine value of i
int i = 0;
double pi = 100;
{ <  Remove this
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);
} <  Remove this
}
 02172011, 06:31 AM #12
Follow javaforum$ advice.
I kinda misread that as a start of while loop, rather than end of dowhile loop. Sorry, but I need some coffee :(
GoldestJava Is A Funny Language... Really!
Click on * and add to member reputation, if you find their advices/solutions effective.
 02172011, 08:18 AM #13
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,039
 Blog Entries
 7
 Rep Power
 23
I have the stamina of a seal; I lie on the beach instead of running on it.
Similar Threads

Help with java math
By gabrielfirestorm in forum New To JavaReplies: 6Last Post: 10252010, 01:54 PM 
Help need on math java program
By zidangus in forum New To JavaReplies: 27Last Post: 07072010, 02:48 AM 
Math Program
By stlboi in forum New To JavaReplies: 8Last Post: 04022009, 09:34 PM 
need help with math for a new program
By gotenks05 in forum New To JavaReplies: 13Last Post: 09262008, 07:32 AM 
Java Math
By levent in forum Java TutorialReplies: 1Last Post: 05122008, 09:03 AM
Bookmarks