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);

    }
  }
}

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.

Be a part of the DaniWeb community

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