fork download
  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. double S, Si, y, x, eps, A, V;
  5. double i, j;
  6. Scanner scanner = new Scanner(System.in);
  7. System.out.println("Enter x (|x|<=1):");
  8. x = scanner.nextDouble();
  9. System.out.println("Enter eps (example: 1e-5):");
  10. eps = scanner.nextDouble();
  11. i = -1.0;
  12. j = 2.0;
  13. Si = x / 2.0;
  14. S = 1.0 + Si;
  15. while (Math.abs(Si) > eps) {
  16. i = i + 2.0;
  17. j = j + 2.0;
  18. Si = Si * (-1.0) * (i / j) * x;
  19. S = S + Si;
  20. }
  21. y = Math.sqrt(1.0 + x);
  22. A = Math.abs(y - S);
  23. V = (A / Math.abs(y)) * 100.0;
  24. System.out.printf("x=%.6f eps=%.6f\n", x, eps);
  25. System.out.printf(" y=%.6f S=%.6f\n", y, S);
  26. System.out.printf(" A=%.5f V=%.5f %%\n", A, V);
  27. scanner.close();
  28. }
  29. }
Success #stdin #stdout 0.16s 57944KB
stdin
0.1
0.00001
stdout
Enter x (|x|<=1):
Enter eps (example: 1e-5):
x=0.100000 eps=0.000010
 y=1.048809  S=1.048809
 A=0.00000 V=0.00002 %