Computer Science, asked by MilindDhara, 3 months ago

s=1/2!-2/3!+3/4!-....n-1/n! solution in java it's from Nested loop please help. I need the answer fast ​

Answers

Answered by anindyaadhikari13
3

Required Answer:-

Question:

Write a program in Java to find the sum of the series.

S = 1/2! - 2/3! + 3/4! - 4/5! +...

Solution:

Here is the program.

  1. import java.util.*;
  2. public class Sum {
  3. public static void main(String[] args) {
  4. int n;
  5. double f=2.0;
  6. double s=0.0;
  7. Scanner sc=new Scanner(System.in);
  8. System.out.print("Enter limit - ");
  9. n=sc.nextInt();
  10. for(int i=1,c=3;i<=n;i++,c++)
  11. {
  12. if(i%2==1)
  13. s+=i/f;
  14. else
  15. s-=i/f;
  16. f * =c;
  17. }
  18. System.out.println("Sum is: "+s);
  19. sc.close();
  20. }
  21. }

Explanation:

  • We will create a loop that iterates n times. Inside the loop, we will calculate the sum of the series. First term of the series is added and the next term is subtracted. This continues upto n terms. Here, the control variable is 'i'. If the value of 'i' is odd, then the term of the series is added else it's subtracted.

Output is attached.

Attachments:

Anonymous: which app do u use for it
anindyaadhikari13: JAVA N-IDE. You can find it on play store.
Anonymous: Thanks
anindyaadhikari13: Welcome.
Similar questions