Untitled

 avatar
unknown
java
2 years ago
1.6 kB
5
Indexable
import java.util.Locale;
import java.util.Scanner;

public class Program {
	public static double abs(double value) {
		return (value < 0.0) ? -value : value;
	}

	public static double an(int n, double previous, double x) {
		return -previous * ((x * x) / n);
	}

	public static void calculateAndPrint(int n, double x, int e) {
		double tempSum = 1;
		double sumOfGreaterThanE = 1;
		double sumOfGreaterThanEOverTen = 1;
		double mathResult = Math.pow(Math.E, -Math.pow(x, 2));
		double previous = 1;
		for (int i = 1; i <= n; i++) {
			double an = an(i, previous, x);
			previous = an;
			tempSum += an;
			if (abs(an) > e) {
				sumOfGreaterThanE += an;
			}
			if (abs(an) > e / 10.0) {
				sumOfGreaterThanEOverTen += an;
			}
		}
		tempSum = abs(tempSum);
		System.out.printf("1) Сумма %d слагаемых: %.6f %n", n, tempSum);
		System.out.printf("2) Сумма слагаемых больше %d: %.3f %n", e, sumOfGreaterThanE);
		System.out.printf("3) Сумма слагаемых больше %.3f: %.3f %n", e / 10.0, sumOfGreaterThanEOverTen);
		System.out.print("4) Значение функции при помощи методов Math: " + mathResult);
	}

	public static void main(String[] args) {
		Locale.setDefault(Locale.ROOT);
		Scanner scanner = new Scanner(System.in);
		/*System.out.print("Введите x: ");
		double x = scanner.nextDouble();
		System.out.print("Введите n: ");
		int n = scanner.nextInt();
		System.out.print("Введите e: ");
		int e = scanner.nextInt();
		calculateAndPrint(n, x, e);*/
		calculateAndPrint(15, 2, 0);
	}
}