Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package grigorieva.olga;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class OAiPVariant7Program4 {
- public static final int MaxSize = 20;
- public static void main(String[] args) throws IOException {
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
- System.out.println("Данная программа вычисляет: -A1/1! + A2/2! -...+ (-1)^N*AN/N!");
- System.out.println("Введите N: ");
- boolean isCorrect = true;
- String inputn = bufferedReader.readLine();
- int n = Integer.parseInt(inputn);
- do {
- System.out.println("Введите N: ");
- try {
- n = Integer.parseInt(inputn);
- if (n > 0 && n < MaxSize) {
- isCorrect = false;
- }
- } catch (Exception e) {
- }
- } while (isCorrect);
- int[] aArray = new int[n];
- for (int i = 0; i < n; i++) {
- System.out.println("Введите A" + (i + 1) + ": ");
- String ai = bufferedReader.readLine();
- aArray[i] = Integer.parseInt(ai);
- }
- double sum = 0;
- for (int i = 0; i < n; i++) {
- double step = 1;
- for (int j = 0; j < i; j++) {
- step = -step ;
- }
- double facto = 1;
- for (int j = 2; j < i + 1; j++) {
- facto = facto * j;
- }
- double znach = step * aArray[i] / facto;
- sum = sum + znach;
- }
- System.out.println("Сумма = " + sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement