Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- System.out.println("Calculate the value of the Pi with a given accuracy using the formula: Pi/8=1/(1*3)+1/(5*7)+1/(9*11)...");
- double epsilon = 0;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- epsilon = Double.parseDouble(scan.nextLine());
- } catch (NumberFormatException e) {
- System.err.println("Enter the double value!");
- isIncorrect = true;
- }
- if (!(isIncorrect || epsilon < 1 && epsilon > 0)) {
- System.err.println("Enter the value in range [0, 1]");
- isIncorrect = true;
- }
- } while (isIncorrect);
- double preStep;
- int counter = 1;
- double pi = 0;
- do {
- preStep = pi;
- pi += (double) 8 / (counter * (counter + 2));
- counter += 4;
- } while (Math.abs((pi - preStep)) > epsilon);
- System.out.printf("The Pi with your accuracy is: %f", pi);
- scan.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement