- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
Following is the Java program to find the sum of the series −
1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
Example
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)); } }
Output
The sum of the series is : 2.7166666666666663
A class named Demo contains a function named ‘pattern_sum’. This function takes a double valued number as parameter, and iterates through the value and calculates the series value of (1/1! + 2/2! + ..) and so on. In the main function, the value is defined and the function ‘pattern_sum’ is called by passing this value. The output is displayed on the console.
Advertisements