Java Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!

JavaServer Side ProgrammingProgramming

Following is the Java program to find the sum of the series −

1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!

Example

 Live Demo

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.

raja
Published on 17-Aug-2020 12:13:53
Advertisements