以下是Java程序来查找序列的总和-
1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
import java.io.*; import java.lang.*; public class Demo{ public static double pattern_sum(double val){ double residual = 0, factorial_val = 1; for (int i = 1; i <= val; i++){ factorial_val = factorial_val * i; residual = residual + (i / factorial_val); } return (residual); } public static void main(String[] args){ double val = 6; System.out.println("The sum of the series is : " + pattern_sum(val)); } }
输出结果
The sum of the series is : 2.7166666666666663
名为Demo的类包含一个名为“ pattern_sum”的函数。此函数将一个双精度值数字作为参数,然后迭代该值并计算序列值(1/1!+ 2/2!+ ..),依此类推。在主函数中,定义了值,并通过传递该值来调用函数“ pattern_sum”。输出显示在控制台上。