Hi again,

I managed to find a big problem in my code -- I didn't increment the
variable of the condition of the while loop so in essence I had an infinite
loop going without knowing I had created it.

But I am still getting output that I am not sure if it is correct ?

This is the output that I am getting :-

Enter number iterations for Pi value: 5
The value you entered was: 5
The value for valuesofar is: 1.0
the value for x is: 3
The value you entered was: 5
The value for valuesofar is: 1.0
the value for x is: 5
The value you entered was: 5
The value for valuesofar is: 1.0
the value for x is: 7
The value you entered was: 5
The value for valuesofar is: 1.0
the value for x is: 9
The value you entered was: 5
The value for valuesofar is: 1.0
the value for x is: 11
The value so far is: 1.0


And I doubt whether 1.0 is the answer that I am supposed to be getting ?

----------------------------------------------------------------------------
-----
The question that I am getting is as follows :-

Approximating the value of pi :-

pi/4 = 1 - 1/3 + 1/5 - 1/7 + ........

Write a program that allows the user to specify the number of iterations
used in this approximation and displays the resulting value ?

My code is as follows and also will be included as an attachment :-
----------------------------------------------------------------------------
----


import TerminalIO.*;

public class Liebniz {

   KeyboardReader reader = new KeyboardReader();
   ScreenWriter writer = new ScreenWriter();

   int i;
   int x;
   int numiter;
   double valuesofar;

//================================================================
   public void run() {


   numiter = reader.readInt("Enter number iterations for Pi value: ");



   i = 1;
   x = 1;
   valuesofar = 0;

   while (i <= numiter) {

      writer.println("The value you entered was: " + numiter);

    if ((i % 2) == 0) { valuesofar -=  1 / x;
    } else {
     valuesofar += 1 / x;
     }

   writer.println("The value for valuesofar is: " + valuesofar);
   x = x + 2;
   writer.println("the value for x is: " + x);
   i++;

  }

   writer.println("The value so far is: " + valuesofar);


   }

//=================================================================
   public static void main(String [] args) {
      Liebniz tpo = new Liebniz();
      tpo.run();
   }
}


----- Original Message -----
From: "Jason Bell" <[EMAIL PROTECTED]>
To: "JDJList" <[EMAIL PROTECTED]>
Sent: Thursday, September 19, 2002 6:15 PM
Subject: [jdjlist] RE: Help Needed


>
> Funny that, I decided to stay in the UK but yearn for Sam Adams
(especially
> Oktoberfest).  I suppose it depends what Theakstons you get out of a
bottle.
> I would steer to Black Sheep, Old Peculiar or Crompton's Two Pints.  The
> beer shop near home stocks Sam Adams Boston Ale which is lovely.
>
> Regards
> Jase
>
>
>
> <Gerry_Smith_wrote>
> I moved to the US and found some investors.....
> have to drink Theakstons out of bottles though...
> Gerry
> </Gerry_Smith_wrote>
>
>
> To change your JDJList options, please visit:
http://www.sys-con.com/java/list.cfm
>

Attachment: Liebniz.java
Description: Binary data

To change your JDJList options, please visit: http://www.sys-con.com/java/list.cfm

Reply via email to