Advertisement
grioool

Untitled

Sep 19th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. package grigorieva.olga;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. public class OAiPVariant7Program4 {
  8. public static final int MaxSize = 20;
  9.  
  10. public static void main(String[] args) throws IOException {
  11. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  12. System.out.println("Данная программа вычисляет: -A1/1! + A2/2! -...+ (-1)^N*AN/N!");
  13. System.out.println("Введите N: ");
  14. boolean isCorrect = true;
  15. String inputn = bufferedReader.readLine();
  16. int n = Integer.parseInt(inputn);
  17. do {
  18. System.out.println("Введите N: ");
  19. try {
  20. n = Integer.parseInt(inputn);
  21. if (n > 0 && n < MaxSize) {
  22. isCorrect = false;
  23. }
  24. } catch (Exception e) {
  25. }
  26. } while (isCorrect);
  27. int[] aArray = new int[n];
  28.  
  29. for (int i = 0; i < n; i++) {
  30. System.out.println("Введите A" + (i + 1) + ": ");
  31. String ai = bufferedReader.readLine();
  32. aArray[i] = Integer.parseInt(ai);
  33. }
  34. double sum = 0;
  35. for (int i = 0; i < n; i++) {
  36.  
  37. double step = 1;
  38. for (int j = 0; j < i; j++) {
  39. step = -step ;
  40. }
  41.  
  42. double facto = 1;
  43. for (int j = 2; j < i + 1; j++) {
  44. facto = facto * j;
  45. }
  46.  
  47. double znach = step * aArray[i] / facto;
  48. sum = sum + znach;
  49. }
  50. System.out.println("Сумма = " + sum);
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement