In the previous article, we have discussed about Java Program to Display the Sum of Series (1+2)/(1*2) + (1+2+3)/(1*2*3) + …… + (1+2+3+…+N)/(1*2*3*…*N)
In this article we are going to see how to print the sum of series 1! + 2! + 3! + 4! + ….. + N! by using Java programming language
Java Program to Print the Sum of Series 1! + 2! + 3! + 4! + ….. + N!
In this series the it can be seen that numbers at each position i , the term is calculated as the factorial of i.
For example:
If at 3rd position the term is 3! = 6 and the sum up to 3rd position will be 1! + 2! + 3! = 1 + 2 + 6 = 9. Here, we for each we have to find the factorial of the number and add it to the sum.
Let’s see how to print the series.
Method-1: Java Program to Print the Sum of Series 1! + 2! + 3! + 4! + ….. + N! By Using User Input Value
Approach:
- Create Scanner class object.
- Prompt the user to enter a number.
- Initialize a variable sum to 0 of long data type(for larger values, int data type may overflow).
- Run a for loop
i=1 to n. - Inside the loop, initialize variable long fact = 1
- Use another nested loop
j=1 to ito find the factorial. - Inside the nested loop, update fact as
fact *= j. - Outside the inner loop and inside the outer loop, update sum as
sum = sum + fact.
Program:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
// create a Scanner object
Scanner sc = new Scanner(System.in);
// prompt the user to enter the number of terms
System.out.print("Enter the number of terms: ");
int n = sc.nextInt();
// call the method to print the series
long sum = 0;
for (int i = 1; i <= n; i++)
{
long fact = 1;
for (int j = 1; j <= i; j++)
{
fact *= j;
}
sum += fact;
}
System.out.print("The sum of the series is: " + sum);
}
}
Output: Enter the number of terms: 3 The sum of the series is: 9
Method-2: Java Program to Print the Sum of Series 1! + 2! + 3! + 4! + ….. + N! By Using User Defined Method
Approach:
- Use the same approach as method 1 but move the nested loop inside a user-defined method
Program:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
// create a Scanner object
Scanner sc = new Scanner(System.in);
// prompt the user to enter the number of terms
System.out.print("Enter the number of terms: ");
int n = sc.nextInt();
// call the method to print the series
long sum = 0;
for (int i = 1; i <= n; i++)
{
sum += fact(i);
}
System.out.print("The sum of the series is: " + sum);
}
private static long fact(int i)
{
long fact = 1;
for (int j = 1; j <= i; j++)
{
fact *= j;
}
return fact;
}
}
Output: Enter the number of terms: 5 The sum of the series is: 153
Method-3: Java Program to Print the Sum of Series 1! + 2! + 3! + 4! + ….. + N! By Calculating Factorial Value Using Recursion
Approach:
Use the same approach as method 2 but the user-defined method will calculate the factorial value recursively.
- Define a method called fact which returns a double value take ‘
n’ as an argument of int data type. - Inside the recursive method, check if (n == 1), return 1 (base condition).
- Else return
n*fact(n-1)
Program:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
// create a Scanner object
Scanner sc = new Scanner(System.in);
// prompt the user to enter the number of terms
System.out.print("Enter the number of terms: ");
int n = sc.nextInt();
// call the method to print the series
long sum = 0;
for (int i = 1; i <= n; i++)
{
sum += fact(i);
}
System.out.print("The sum of the series is: " + sum);
}
// method to calculate factorial recursively
private static long fact(int n)
{
// base condition
if (n == 1)
return 1;
return n * fact(n-1);
}
}
Output: Enter the number of terms: 7 The sum of the series is: 5913
Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.
Related Java Programs: