Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.82 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4. import java.lang.Math;
  5.  
  6. public class Main {
  7.  
  8.     public static void main(String[] args) {
  9.         Scanner inConsole = new Scanner(System.in);
  10.         System.out.print("Введите число n = ");
  11.         int n = inConsole.nextInt();
  12.         while (n == 0) {
  13.           System.out.print("ОШИБКА! Введит чило n>0 = ");
  14.           n = inConsole.nextInt();
  15.         }
  16.  
  17.         System.out.print("Введите число x = ");
  18.         double x = inConsole.nextDouble();
  19.  
  20.         System.out.println(counterSUM(n, x));
  21.  
  22.         System.out.print(" Введите e = ");
  23.         double e = inConsole.nextDouble();
  24.         while (e > 1) {
  25.             System.out.print("ОШИБКА! Введите е<1: ");
  26.             e = inConsole.nextDouble(); }
  27.  
  28.         System.out.println(counterSumWithE(e, x));
  29.         System.out.println(counterSumWithE(e / 10, x));
  30.  
  31.         System.out.print(counterSumWithMath(x));
  32.     }
  33.  
  34.     static double parameters (double fraction , int i, double x) {
  35.         fraction = fraction * x / i;
  36.         return fraction;
  37.     }
  38.  
  39.     static double counterSUM(int n, double x) {
  40.         double first = 1;
  41.         double amount = 0;
  42.         for (int i = 1; i <= n; i++) {
  43.             amount += first;
  44.             first = parameters (first, i, x);
  45.         }
  46.         return amount;
  47.     }
  48.  
  49.     static double counterSumWithE(double e, double x) {
  50.         double fractionNew = 1;
  51.         int k = 1;
  52.         double amount = 0;
  53.         while (Math.abs(parameters(fractionNew, k, x)) > e ) {
  54.             amount += fractionNew;
  55.             k++;
  56.             fractionNew = parameters(fractionNew, k, x);
  57.         }
  58.         return amount;
  59.     }
  60.  
  61.     static double counterSumWithMath(double x) {
  62.         return Math.pow(Math.E, x);
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement