Created
June 11, 2020 08:21
-
-
Save Khuzha/c4b8807d63d20ff8d4bc2b9fd6e79976 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.khuzha.objects; | |
import java.util.Scanner; | |
public class Sergey { | |
static int factorial(int n) { | |
if (n == 0) | |
return 1; | |
else | |
return(n * factorial(n-1)); | |
} | |
private static float calc (int k) { | |
float res = (float) (Math.pow(-1, k) / factorial(k)); | |
return res; | |
} | |
public static void main(String[] args) { | |
Scanner scan = new Scanner(System.in); | |
float e = (float) (0.5 * Math.pow(10, -4)); | |
float a = calc(1); | |
float sum = a; | |
float prev = 0f; | |
System.out.println("e = " + e); | |
for (int k = 2; Math.abs(a - prev) > e; k++) { | |
System.out.println("a = " + a + " prev = " + prev + " sum = " + sum); | |
prev = a; | |
a = calc(k); | |
sum += a; | |
} | |
System.out.printf("Sum = " + sum + "a = " + a + "\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment