用Java程序找到1/1系列的总和!+ 2/2!+ 3/3!+ 4/4!+……。+ n / n!
以下是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”。输出显示在控制台上。
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短