Find the Sum of the series 1 + 1/3 + 1/5 + 1/7 + … till N terms

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

1 + \frac{1}{3} + \frac{1}{5} + \frac{1}{7} + ...

Examples:

Input: N = 10
Output: 2.133256
Explanation:
The sum of series 1 + 1/3 + 1/5 + 1/7 + 1/9 + 1/11 is 2.133256.

Input: N = 20
Output: 2.479674
Explanation:
The sum of series 1 + 1/3 + 1/5 + 1/7 + … + 1/41 is 2.479674.



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

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

Therefore:

Nth term of the series 1 + \frac{1}{3} + \frac{1}{5} + \frac{1}{7} + ... = \frac{1}{2 * 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 to find the sum of the
// series 1 + 1/3 + 1/5 + ...
  
#include <iostream>
using namespace std;
  
// Function to find the sum of the
// given series
void printSumSeries(int N)
{
    // Intialise the sum to 0
    float sum = 0;
  
    for (int i = 1; i <= N; i++) {
  
        // Generate the ith term and
        // add it to the sum
        sum += 1.0 / (2 * i - 1);
    }
  
    // Print the final sum
    cout << sum << endl;
}
  
// Driver Code
int main()
{
    int N = 6;
  
    printSumSeries(N);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the sum of the
// series 1 + 1/3 + 1/5 + ...
class GFG {
      
    // Function to find the sum of the
    // given series
    static void printSumSeries(int N)
    {
        // Intialise the sum to 0
        float sum = 0;
      
        for (int i = 1; i <= N; i++) {
      
            // Generate the ith term and
            // add it to the sum
            sum += 1.0 / (2 * i - 1);
        }
      
        // Print the final sum
        System.out.println(sum);
    }
      
    // Driver Code
    public static void main (String[] args)
    {
        int N = 6;
      
        printSumSeries(N);
  
    }
      
}
  
// This code is contributed by AnkitRai01

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find the sum of the
# series 1 + 1/3 + 1/5 + ...
  
# Function to find the sum of the
# given series
def printSumSeries(N) :
  
    # Intialise the sum to 0
    sum = 0;
  
    for i in range(1, N + 1) :
  
        # Generate the ith term and
        # add it to the sum
        sum += 1.0 / (2 * i - 1);
  
    # Print the final sum
    print(sum);
  
# Driver Code
if __name__ == "__main__" :
  
    N = 6;
  
    printSumSeries(N);
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the sum of the
// series 1 + 1/3 + 1/5 + ...
using System;
  
class GFG {
      
    // Function to find the sum of the
    // given series
    static void printSumSeries(int N)
    {
        // Intialise the sum to 0
        float sum = 0;
      
        for (int i = 1; i <= N; i++) {
      
            // Generate the ith term and
            // add it to the sum
            sum += (float)1.0 / (2 * i - 1);
        }
      
        // Print the final sum
        Console.WriteLine(sum);
    }
      
    // Driver Code
    public static void Main (string[] args)
    {
        int N = 6;
      
        printSumSeries(N);
    }    
}
  
// This code is contributed by AnkitRai01

chevron_right


Output:

1.87821

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




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.



Improved By : AnkitRai01