Write a java programme to find the sum of series:
S=1/2-2/3+3/4-4/5.....-10/11
plzzzz try to answer fast if u know the full answer
it is urgent
class 10 icse board 2020-2021
Answers
Answered by
2
Required Answer:-
Question:
Write a Java program to display the sum of the series
½ - ⅔ + ¾ ...... - 10/11
Solution:
Here is the code.
- public class SumOfSeries {
- public static void main(String[] args) {
- double s=0;
- for(int i=1;i<=10;i++)
- {
- if(i%2==1)
- s+=i/(double)(i+1);
- else
- s-=i/(double)(i+1);
- }
- System.out.println("Sum: "+s);
- }
- }
Explanation:
- Each term of the series is in the form of n/(n + 1) where n = 1,2,3,..10.The series goes in an alternating fashion. First term is added to the sum and then the next term is subtracted from the sum. We have created a loop that iterates 10 times (i = 1 to 10). Here the control variable is 'i'. If 'i' is odd, then we will add the value else we will subtract.
Output is attached.
Attachments:
Similar questions