I need help on this question :(. I cant seem to get the right output for this question.

5.25
(Compute pi) You can approximate pi by using the following series: p=4(1- 1/3 + 1/5 - 1/7 + 1/9 - 1/11..... +((-1)^i+1/2i-1)
Write a program that displays the p value for i = 10000, 20000, ..., and
100000.

``````{

public static void main(String[] args)
{
for (int i = 10000; i <= 100000; i += 10000)
{

//Declare Values
double equation = 1.0;

{
//Equation
equation = (Math.pow(-1.0, i + 1)/(2 * i - 1));
}

// Formula
double pi = 4.0 * equation;

// Output Pi

System.out.println("i = " + i + ". The PI is " + pi);

}
}
}
``````
2
Contributors
1
21
Views
3 Years
Discussion Span
Last Post by Schol-R-LEA

The thing is, when computing a series like this, you can't simply jump to the nth iteration and get the right answer; you need to sum all the iterations before it to get the approximation. You want your `for()` loop to start at 1 and iterate by one, and have an `if()` that checks whether you've reached the one of the points you are to print the approximation out at.

Also, you want to declare `pi` outside of the loop; otherwise, it will get reset on each iteration.

Edited by Schol-R-LEA

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.