📜  Java程序找到Series 1/1的总和! + 2/2! + 3/3! + 4/4! +……。+ n / n!

📅  最后修改于: 2021-05-04 09:05:25             🧑  作者: Mango

您获得了1/1系列! + 2/2! + 3/3! + 4/4! +……。+ n / n !,找出直到第n个项的序列之和。

例子:

Input :n = 5
Output : 2.70833

Input :n = 7
Output : 2.71806
// Java program to print the sum of series
  
import java.io.*;
import java.lang.*;
  
class GFG {
    public static double sumOfSeries(double num)
    {
        double res = 0, fact = 1;
        for (int i = 1; i <= num; i++) {
            /*fact variable store factorial of the i.*/
            fact = fact * i;
  
            res = res + (i / fact);
        }
        return (res);
    }
  
    public static void main(String[] args)
    {
        double n = 5;
        System.out.println("Sum: " + sumOfSeries(n));
    }
}
  
// Code contributed by Mohit Gupta_OMG <(0_o)>
输出:
Sum: 2.708333333333333

请参阅有关程序的完整文章以找到1/1系列的总和! + 2/2! + 3/3! + 4/4! +……。+ n / n!更多细节!