Exercise ComputePI (Loop & Condition) ~ Programming is Fun
 Programming is Fun!

www.anandsingh.net
Subscribe For Free Updates!

We'll not spam mate! We promise.

Saturday, September 6, 2014

Exercise ComputePI (Loop & Condition)

Write a program called ComputePI to compute the value of π, using the following series expansion. You have to decide on the termination criterion used in the computation (such as the number of terms used or the magnitude of an additional term). Is this series suitable for computing π?
ExerciseBasics_ComputePI.png
JDK maintains the value of π in a double constant called Math.PI. Compare the values obtained and the Math.PI, in percents of Math.PI.
Hint: Add to sum if the denominator modulus 4 is 1, and subtract from sum if it is 3.
double sum = 0;
int maxDenom = 10000000;
for (int denom = 1; ..... ; denom = denom + 2) {
   if (denom % 4 == 1) {
      sum += ......;
   } else if (denom % 4 == 3) {
      sum -= ......;
   } else {
      System.out.println("The computer has gone crazy?!");
   }
}
Comment your answers :)


Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →

0 comments:

Post a Comment