Find the Sum of the series 1/2 - 2/3 + 3/4 - 4/5 + ... till N terms - GeeksforGeeks

24 Nov, 2021
Given a number N, the task is to find the sum of the below series till N terms. 
Examples:  
Input: N = 6 Output: -0.240476Input: 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 
*** QuickLaTeX cannot compile formula:
 

*** Error message:
Error: Nothing to show, formula is empty

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++
Java
Python3
C#
Javascript
// C++ program for the above approach #include <bits/stdc++.h>using namespace std; // Function to find the sum of seriesvoid 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 Codeint main(){    int N = 10;     printSeriesSum(N);    return 0;}
// Java program for the above approachclass GFG{  // Function to find the sum of seriesstatic 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 Codepublic static void main(String[] args){    int N = 10;      printSeriesSum(N);}} // This code is contributed by 29AjayKumar
# Python3 program for the above approach # Function to find the sum of seriesdef 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 Codeif __name__ == "__main__" :     N = 10;     printSeriesSum(N);        # This code is contributed by Yash_R
// C# program for the above approachusing System;  class GFG {      // Function to find the sum of seriesstatic 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
<script> // javascript program for the above approach // Function to find the sum of seriesfunction printSeriesSum( N){    let sum = 0;     for (let 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 += i / (i + 1);        }        else {            sum -= i / (i + 1);        }    }     // Print the sum     document.write( sum.toFixed(6) );} // Driver Code     let N = 10;     printSeriesSum(N);    // This code is contributed by todaysgaurav </script>
-0.263456
 
Time Complexity: O(N)
Auxiliary Space: O(1)
29AjayKumar
shivanisinghss2110
Yash_R
todaysgaurav
rishavmahato348
series-sum
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Find all factors of a natural number | Set 1
Program to find sum of elements in a given array
Python Dictionary
Arrays in C/C++
Reverse a string in Java
Inheritance in C++
Constructors in C++