Computer Science, asked by allstar3, 3 months ago

Q2. Write a program In java​

Attachments:

Answers

Answered by BrainlyProgrammer
3

Question:

  • WAP in Java to print the sum of this series.

 \sf\dfrac{1!}{x}  -  \dfrac{2!}{x}  +  \dfrac{3!}{x}  -  \dfrac{4!}{x} ...n

Answer:

import java.util.*;

public class Program

{

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("Enter n and x");

int x=sc.nextInt();

int n=sc.nextInt();

double s=0,fact=1;

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

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

fact*=j;

}

s=(i%2==0)?(s+fact)/x:(s-fact)/x;

}

System.out.println("Sum:"+s);

}

}

•Output Attached

•••♪

Attachments:
Similar questions