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
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.
- import java.util.*;
- public class Sum {
- public static void main(String[] args) {
- int n;
- double f=2.0;
- double s=0.0;
- Scanner sc=new Scanner(System.in);
- System.out.print("Enter limit - ");
- n=sc.nextInt();
- for(int i=1,c=3;i<=n;i++,c++)
- {
- if(i%2==1)
- s+=i/f;
- else
- s-=i/f;
- f * =c;
- }
- System.out.println("Sum is: "+s);
- sc.close();
- }
- }
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
Similar questions