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)
- {
- Scanner in = new Scanner(System.in);
- double den = 1;
- double iteration = 1;
- double calcPi = 0;
- int user;
- /*prompt user input
- if input is valid, continue
- else request input again
- */
- System.out.println("put number");
- user = in.nextInt();
- if(user <=6 && user >= 1)
- {
- do
- {
- if(iteration % 2 ==0)
- {
- //negative fraction
- calcPi -= 1/den;
- }
- else
- {
- //positive fraction
- calcPi += 1/den;
- }
- iteration++;
- den += 2;
- }
- while(iteration <= 2);
- }
- System.out.println("~ alignment bar ~ 1234567890abcdefghij");
- System.out.println("Computed value of π : " + calcPi*4);
- System.out.println("Expected value of π : " + Math.PI);
- System.out.println("Required iterations : " + iteration);
- System.out.println("Required threshold : ");
- System.out.println("Desired accuracy : " + user);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement