Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class ComputePI {
- public static void main(String[] args) {
- System.out.print( "Please enter the amount of "
- + "digits of PI, you would like to set it too");
- Scanner termScan = new Scanner( System.in );
- int term = termScan.nextInt();
- termScan.close();
- double pi = 3.0;
- int loopCount = 2;
- int number = 2;
- while ( loopCount <= term )
- {
- if (loopCount % 2 == 0)
- {
- pi = pi + ( 4.0/ ((number)*(number+1)*(number+2)) );
- }
- else
- {
- pi = pi - ( 4.0 / ((number)*(number+1)*(number+2)));
- }
- number = number + 2;
- loopCount++;
- continue;
- }
- System.out.print( "The Value of Pi in " + term +
- " terms is equal to " + pi);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement