Find the Sum of the series 1/2 – 2/3 + 3/4 – 4/5 + … till N terms

Given a number N, the task is to find the sum of the below series till N terms.

\frac{1}{2} - \frac{2}{3} + \frac{3}{4} - \frac{4}{5} + ...

Examples:

Input: N = 6
Output: -0.240476

Input: N = 10
Output: -0.263456



Approach: From the given series, find the formula for Nth term:

1st term = 1/2
2nd term = - 2/3
3rd term = 3/4
4th term = - 4/5
.
.
Nthe term = ((-1)N) * (N / (N + 1))

Therefore:

Nth term of the series \frac{1}{2} - \frac{2}{3} + \frac{3}{4} - \frac{4}{5} + ... = (-1)^{N}\times \frac{N}{N+1}

Then iterate over numbers in the range [1, N] to find all the terms using the above formula and compute their sum.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the sum of series
void printSeriesSum(int N)
{
    double sum = 0;
  
    for (int i = 1; i <= N; i++) {
  
        // Generate the ith term and
        // add it to the sum if i is
        // even and subtract if i is
        // odd
        if (i & 1) {
            sum += (double)i / (i + 1);
        }
        else {
            sum -= (double)i / (i + 1);
        }
    }
  
    // Print the sum
    cout << sum << endl;
}
  
// Driver Code
int main()
{
    int N = 10;
  
    printSeriesSum(N);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
class GFG{
   
// Function to find the sum of series
static void printSeriesSum(int N)
{
    double sum = 0;
   
    for (int i = 1; i <= N; i++) {
   
        // Generate the ith term and
        // add it to the sum if i is
        // even and subtract if i is
        // odd
        if (i % 2 == 1) {
            sum += (double)i / (i + 1);
        }
        else {
            sum -= (double)i / (i + 1);
        }
    }
   
    // Print the sum
    System.out.print(sum +"\n");
}
   
// Driver Code
public static void main(String[] args)
{
    int N = 10;
   
    printSeriesSum(N);
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach
  
# Function to find the sum of series
def printSeriesSum(N) :
      
    sum = 0;
  
    for i in range(1, N + 1) :
  
        # Generate the ith term and
        # add it to the sum if i is
        # even and subtract if i is
        # odd
        if (i & 1) :
            sum += i / (i + 1);
       
        else :
            sum -= i / (i + 1);
      
  
    # Print the sum
    print(sum);
  
# Driver Code
if __name__ == "__main__" :
  
    N = 10;
  
    printSeriesSum(N);
     
    # This code is contributed by Yash_R

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using System;
   
class GFG {
       
// Function to find the sum of series
static void printSeriesSum(int N)
{
    double sum = 0;
  
    for (int i = 1; i <= N; i++) {
  
        // Generate the ith term and
        // add it to the sum if i is
        // even and subtract if i is
        // odd
        if ((i & 1)==0) {
            sum += (double)i / (i + 1);
        }
        else {
            sum -= (double)i / (i + 1);
        }
    }
  
    // Print the sum
    Console.WriteLine(sum);
}
  
// Driver Code
    public static void Main (string[] args)
    {
         
    int N = 10;
  
    printSeriesSum(N);
}
}
  
// This code is contributed by shivanisinghss2110

chevron_right


Output:

-0.263456

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.