116. JAVA Program to Compute the 1!+2!+3!+ 4!+ …….+n Series.
How does this program work?
- This program is used to find the Sum of the given series 1!+2!+3!+ 4!+ …….+n using java.
- The integer entered by the user is stored in variable n i.e., last term for the given series.
- Declare variable sum and Initially initialize with 0, And also Declare another variable fact initialize with 1.
- First we need to calculate the factorial of number after that performed by sum for n terms.
Here is the code
//To Find the Sum of given series 1!+2!+3!+ 4!+ …….+n
import java.util.Scanner;public class Series
{
public static void main(String[] args )
{
int total=0,i=1,n;
System.out.print("Enter number :\n" );
Scanner skill = new Scanner(System.in);
n = skill.nextInt();
// calculate factorial here.
while ( i <= n ){
int factorial=1;
int j=1;
while ( j <= i )
{
factorial = factorial*j;
j = j+1;
}
// calculate sum of factorial of the number.
total = total + factorial;i = i+1;
}
// print the result here.
System.out.println("Sum is : " + total);}
}