Hi Im writing a beginner program that computes the value of Pi ( pi/4 = 1 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 +... ). I've done this one, but now I want to find out how many terms do i have to use beofre i first get a value that begins with 3.14159

this is the Pi number

``````public class piBegins

{
public static void main(String[]args)
{

double sum = 1.0;
for(double i = 1; i <= 200000; i++)
sum += Math.pow(-1,i)*1.0/(2.0*i + 1.0);

System.out.printf("Pi = %s", sum*4);
}
}``````

first thing i can think of is assigning the value for sum = 3.14159

## All 4 Replies

Why do it the hard way when there's already a constant for that in the math class (Math.PI) :icon_wink:

yes indeed it is a homework :D

well i think i may not need to do the reverse thing, here im trying to print all the 200,000 terms and see where the value apears to be 3.14159... but i cant :(
btw how do i set the range for i (ie. i want it to run from i=5 to i=10, when i change double i = 5, the outputs are wrong...)

``````public class piBegins

{
public static void main(String[]args)  {

System.out.printf( "%s\t %s\n\n", "terms", "Pi");

double sum = 1.0;
for(int i = 1; i <= 117000; i++)
{
sum += Math.pow(-1,i)*1.0/(2.0*i + 1.0);

System.out.printf( "%s\t %.5f\n", i, sum*4);
}
}
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.