Computer Science, asked by saqlain111, 1 year ago

WAP series WAP to print the series 2/3-4/5+6/7......... n terms
in java​

Answers

Answered by suskumari135
0

WAP to print the series

Explanation

Program:

Given the value of n, compute the sum of the series (2 / 3) – (4 / 5) + (6 / 7) – (8 / 9) + – – – – – – – upto n terms.

class Main {

   static double sumSeries(int n)  

   {  

   int i = 1;  

   double r = 0.0;  

   boolean s = true;  

   while (n > 0)  

   {  

       n--;  

       if (s)   {  

           s = !s;  

           r = r + (double)++i / ++i;    }  

       else    {  

           s = !s;  

           r = r - (double)++i / ++i;   }  

   }  

   return r;  

}  

   public static void main (String[] args) {  

       int number = 5;  

       System.out.print("sum of series= "+sumSeries(number));  

   }  

}

Output:

sum of series= 0.7440115440115439  

Attachments:
Similar questions