Advertisement
Roughosing

Untitled

Oct 25th, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.78 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class ComputePI {
  4.    
  5.     public static void main(String[] args) {
  6.        
  7.         System.out.print( "Please enter the amount of  "
  8.                 + "digits of PI, you would like to set it too");
  9.         Scanner termScan = new Scanner( System.in );
  10.         int term = termScan.nextInt();
  11.         termScan.close();
  12.         double pi = 3.0;
  13.         int loopCount = 2;
  14.         int number = 2;
  15.        
  16.        
  17.             while ( loopCount <= term )
  18.             {
  19.                     if (loopCount % 2 == 0)
  20.                 {
  21.                     pi = pi + ( 4.0/ ((number)*(number+1)*(number+2)) );
  22.                 }
  23.                 else
  24.                 {
  25.                     pi = pi - ( 4.0 / ((number)*(number+1)*(number+2)));
  26.                 }
  27.                
  28.                 number = number + 2;
  29.                
  30.                 loopCount++;
  31.                 continue;
  32.             }
  33.            
  34.            
  35.            
  36.         System.out.print( "The Value of Pi in " + term +
  37.                     " terms is equal to " + pi);
  38.     }
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement