In the previous article, we have discussed about Java Program to Find the Sum of Series (1/1!) + (2/2!) + …… + (N/N!)
In this article we are going to see how to find the sum of series 1!/1 + 2!/2 + 3!/3 + 4!/4+ 5!/5 …. + N!/N by using Java programming language.
Java Program to Find the Sum of Series 1!/1 + 2!/2 + 3!/3 + 4!/4+ 5!/5 …. + N!/N
On observing the pattern carefully, we can see clearly first term starts from 1 and goes up to N
and each term is represented by factorial of that number divided by the number like if ‘a
‘ is the first number then the term is like a!/a
Example:
N = 5 (1!/1) + (2!/2) +(3!/3) + (4!/4) + (5!/5) = >1/1 + 2/2 + 6/3 + 24/4 + 120/5 = >1 + 1 + 2 + 6 + 24 = 34
Let’s see different ways to find the sum of series 1!/1 + 2!/2 + 3!/3 + 4!/4+ 5!/5 …. + N!/N.
Method-1: Java Program to Find the Sum of Series 1!/1 + 2!/2 + 3!/3 + 4!/4+ 5!/5 …. + N!/N By Using For Loop
Approach:
- Declare an int variable say ‘
n
’ which holds the last term value of the series - Create Scanner class object.
- Prompt the user to enter a number as value of
n
. - Use a for loop from i=1 to i<=n
- Inside loop find value of each term and keep track on sum.
- Print the result.
Program:
import java.util.*; public class Main { public static void main(String args[]) { //creating object of Scanner class Scanner s = new Scanner(System.in); //Taking input of number of elements in the series System.out.println("Enter the value of last term 'n' "); int n = s.nextInt(); //for loop to print the series int fact=1; double sum=0; for(int i=1;i<=n;i++) { fact*=i; sum+=(double)fact/i; } System.out.println("Sum of the series is "+sum); } }
Output: Enter the value of last term 'n' 5 Sum of the series is 34.0
Method-2: Java Program to Find the Sum of Series 1!/1 + 2!/2 + 3!/3 + 4!/4+ 5!/5 …. + N!/N By Using While Loop
Approach:
- Declare an int variable say ‘
n
’ which holds the last term value of the series - Create Scanner class object.
- Prompt the user to enter a number as value of
n
. - Iterate a while loop from i=1 till i<=n
- Inside loop find value of each term and keep track on sum.
- Print the result.
Program:
import java.util.*; public class Main { public static void main(String args[]) { //creating object of Scanner class Scanner s = new Scanner(System.in); //Taking input of number of elements in the series System.out.println("Enter the value of last term 'n' "); int n = s.nextInt(); int i=1,fact=1; double sum=0; //using while loop to print the series while (i<=n) { fact*=i; sum+=(double)fact/i; i++; } System.out.println("Sum of the series is "+sum); } }
Output: Enter the value of last term 'n' 5 Sum of the series is 34.0
Method-3: Java Program to Find the Sum of Series 1!/1 + 2!/2 + 3!/3 + 4!/4+ 5!/5 …. + N!/N By Using User Defined Method
Approach:
- The same logic as method 1 but this time we are moving the for inside a user-defined method.
- Create Scanner class object.
- Prompt the user to enter the value “n”
- Call a method to execute the sum series
- Use a for loop from i=1 to i<=n
- Inside loop find value of each term and keep track on sum.
- Print the result.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // creating object of scanner class Scanner s = new Scanner(System.in); //Taking input of number of elements in the series System.out.println("Enter the value of last term 'n' "); int n = s.nextInt(); // calling m1 method to print the series m1(n); } public static void m1(int n) { //for loop to print the series double sum = 0; int fact=1; for(int i=1;i<=n;i++) { fact*=i; sum+=(double)fact/i; } System.out.println("Sum of the series is "+sum); } }
Output: Enter the value of last term 'n' 10 Sum of the series is 409114.0
Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.
Related Java Programs: