Computer Science, asked by pushkarraj0980, 1 day ago

WAP TO PRINT THE FOLLOWING PATTERN
i) S=1! +2!+3!+…………..n!
( IN JAVA )
*Can anyone EXPLAIN the pattern and Give a solid answer .

Answers

Answered by allysia
12

Langauge:

JAVA

Program:

import java.util.Scanner;

public class Program

{public static void main(String[] args)

{Scanner scan = new Scanner (System.in);

int n = scan.nextInt();

int sum=0;

for(int i=1;i<=n;++i){

int product= 1;

for(int j= 1;j<=i;++j){product = product* j;}

sum= sum+ product;}

System.out.println(sum); }}

Logic:

  • Take n input, initialise sum.
  • Run a loop for number of terms and initialise product.
  • Run a nested loop to get the factorials.
  • Add the factorials per loop to sum.
  • Print final sum.

Attachment:

(Output for n = 4)

PS. sorry for the gaseous answer T_T

Attachments:

TheMoonlìghtPhoenix: Awesome!
Similar questions