Advertisement
Vanya_Shestakov

Untitled

Sep 12th, 2021
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. public static void main(String[] args) {
  2. Scanner scan = new Scanner(System.in);
  3. 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)...");
  4. double epsilon = 0;
  5. boolean isIncorrect;
  6. do {
  7. isIncorrect = false;
  8. try {
  9. epsilon = Double.parseDouble(scan.nextLine());
  10. } catch (NumberFormatException e) {
  11. System.err.println("Enter the double value!");
  12. isIncorrect = true;
  13. }
  14.  
  15. if (!(isIncorrect || epsilon < 1 && epsilon > 0)) {
  16. System.err.println("Enter the value in range [0, 1]");
  17. isIncorrect = true;
  18. }
  19. } while (isIncorrect);
  20.  
  21. double preStep;
  22. int counter = 1;
  23. double pi = 0;
  24. do {
  25. preStep = pi;
  26. pi += (double) 8 / (counter * (counter + 2));
  27. counter += 4;
  28. } while (Math.abs((pi - preStep)) > epsilon);
  29.  
  30. System.out.printf("The Pi with your accuracy is: %f", pi);
  31. scan.close();
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement