Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class computepi {
  3. public static void main(String[] args)
  4. {
  5. Scanner in = new Scanner(System.in);
  6.  
  7. double den = 1;
  8. double iteration = 1;
  9. double calcPi = 0;
  10. int user;
  11. /*prompt user input
  12. if input is valid, continue
  13. else request input again
  14. */
  15. System.out.println("put number");
  16. user = in.nextInt();
  17. if(user <=6 && user >= 1)
  18. {
  19. do
  20. {
  21. if(iteration % 2 ==0)
  22. {
  23. //negative fraction
  24. calcPi -= 1/den;
  25. }
  26. else
  27. {
  28. //positive fraction
  29. calcPi += 1/den;
  30. }
  31. iteration++;
  32. den += 2;
  33. }
  34. while(iteration <= 2);
  35. }
  36. System.out.println("~ alignment bar ~ 1234567890abcdefghij");
  37. System.out.println("Computed value of π : " + calcPi*4);
  38. System.out.println("Expected value of π : " + Math.PI);
  39. System.out.println("Required iterations : " + iteration);
  40. System.out.println("Required threshold : ");
  41. System.out.println("Desired accuracy : " + user);
  42.  
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement