Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.0 kB
2
Indexable
Never
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Введите n: ");
        int n = scanner.nextInt(); // Замените значение n на нужное
        double result = calculateExpression(n);
        System.out.println("Результат выражения для n = " + n + ": " + result);
    }

    private static double calculateExpression(int n) {
        double sum = 0;

        for (int i = 1; i <= n; i++) {
            double term = Math.pow(-1, i) * (i + 1) / factorial(i);
            sum += term;
        }

        return sum;
    }

    private static double factorial(int num) {
        if (num == 0 || num == 1) {
            return 1;
        }

        double result = 1;
        for (int i = 2; i <= num; i++) {
            result *= i;
        }

        return result;
    }
}
Leave a Comment