Jav program to print the series 1!+2!+3!+4!.....n!
Answers
import java.util.Scanner;
public class SumOfFactorial
{
public static void main(String[] args)
{
// create scanner class object.
Scanner sc = new Scanner(System.in);
// enter the number.
System.out.print("Enter number : ");
int n = sc.nextInt();
int total=0;
int i=1;
// 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 : " + total);
}
}
Explanation:
class sum
{
p.s.v.m.(int n)
{
int i,s=0,j,f;
for ( i =1;i<=n;i++)
{
f=1;
for ( j=1;j<=i;j++)
{
f=f*j;
}
s=s+f;
}
S.o.pln("the sum " +s)
}
}