Sum of series 2/3 – 4/5 + 6/7 – 8/9 + ——- upto n terms

25 May, 2022
Given the value of n, find the sum of the series (2 / 3) – (4 / 5) + (6 / 7) – (8 / 9) + – – – – – – – upto n terms.Examples :  
Input : n = 5
Output : 0.744012
Series : (2 / 3) - (4 / 5) + (6 / 7) - (8 / 9) + (10 / 11)

Input : n = 7
Output : 0.754268
Series : (2 / 3) - (4 / 5) + (6 / 7) - (8 / 9) +
         (10 / 11) - (12 / 13) + (14 / 15)
 
 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find// sum of given series#include <bits/stdc++.h>using namespace std; // Function to find sum of series// up-to n termsdouble seriesSum(int n){    // initializing counter by 1    int i = 1;         // variable to calculate result    double res = 0.0;    bool sign = true;         // while loop until nth term    // is not reached    while (n > 0)    {        n--;                 // boolean type variable        // for checking validation        if (sign) {            sign = !sign;            res = res + (double)++i / ++i;        }        else {            sign = !sign;            res = res - (double)++i / ++i;        }    }         return res;} // Driver Codeint main(){    int n = 5;    cout << seriesSum(n);       return 0;}
// Java program to find// sum of given seriesimport java.io.*; class GFG {         // Function to find sum of series    // up-to n terms    static double seriesSum(int n)    {         // initializing counter by 1    int i = 1;         // variable to calculate result    double res = 0.0;    boolean sign = true;         // while loop until nth term    // is not reached    while (n > 0)    {        n--;                 // boolean type variable        // for checking validation        if (sign)        {            sign = !sign;            res = res + (double)++i / ++i;        }                 else        {            sign = !sign;            res = res - (double)++i / ++i;        }    }         return res;}         // Driver Code    public static void main (String[] args) {                 int n = 5;                 System.out.print(seriesSum(n));    }} // This code is contributed by vt_m
# Python3 program to find# sum of given series # Function to find# sum of series# up-to n termsdef seriesSum(n):         # initializing    # counter by 1    i = 1;         # variable to    # calculate result    res = 0.0;    sign = True;         # while loop until nth    # term is not reached    while (n > 0):        n = n - 1;                 # boolean type variable        # for checking validation        if (sign):            sign = False;            res = res + (i + 1) / (i + 2);            i = i + 2;        else:            sign = True;            res = res - (i + 1) / (i + 2);            i = i + 2;         return res; # Driver Coden = 5;print(round(seriesSum(n), 6)); # This code is contributed# by mits
// C# program to find// sum of given seriesusing System; class GFG {         // Function to find sum of    // series up-to n terms    static double seriesSum(int n)    {         // initializing counter by 1    int i = 1;         // variable to calculate result    double res = 0.0;    bool sign = true;         // while loop until nth term    // is not reached    while (n > 0)    {        n--;                 // boolean type variable        // for checking validation        if (sign)        {            sign = !sign;            res = res + (double)++i / ++i;        }                 else        {            sign = !sign;            res = res - (double)++i / ++i;        }    }         return res;}         // Driver Code    public static void Main () {                 int n = 5;        Console.Write(seriesSum(n));    }} // This code is contributed by vt_m
<?php// PHP program to find// sum of given series // Function to find sum of series// up-to n termsfunction seriesSum($n){    // initializing counter by 1    $i = 1;         // variable to calculate result    $res = 0.0;    $sign = true;         // while loop until nth term    // is not reached    while ($n > 0)    {        $n--;                 // boolean type variable        // for checking validation        if ($sign) {            $sign = !$sign;            $res = $res + (double)++$i / ++$i;        }        else {            $sign = !$sign;            $res = $res - (double)++$i / ++$i;        }    }         return $res;} // Driver Code$n = 5;echo(seriesSum($n)); // This code is contributed by Ajit.?>
<script> // javascript program to find// sum of given series // Function to find sum of series// up-to n termsfunction seriesSum( n){    // initializing counter by 1    let i = 1;         // variable to calculate result    let res = 0.0;    let sign = true;         // while loop until nth term    // is not reached    while (n > 0)    {        n--;                 // boolean type variable        // for checking validation        if (sign) {            sign = !sign;            res = res + ++i / ++i;        }        else {            sign = !sign;            res = res - ++i / ++i;        }    }         return res;}// Driver Codelet n = 5 ;   document.write(seriesSum(n).toFixed(6)) ;    // This code contributed by aashish1995 </script>
Output : 
0.744012
Time Complexity: O(n), where n represents the given integer.Auxiliary Space: O(1), no extra space is required, so it is a constant.
jit_t
Mithun Kumar
aashish1995
tamanna17122007
series
series-sum
Mathematical
School Programming
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Merge two sorted arrays with O(1) extra space
Program to print prime numbers from 1 to N.
Find next greater number with same set of digits
Segment Tree | Set 1 (Sum of given range)
Python Dictionary
Reverse a string in Java
Arrays in C/C++
Introduction To PYTHON
Interfaces in Java