Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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 

*** 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++

// 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)
{
    // Initialise 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;
}

                    

Java

// 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)
    {
        // Initialise 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

                    

Python3

# 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) :
 
    # Initialise 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

                    

C#

// 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)
    {
        // Initialise 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

                    

Javascript

<script>
 
// javascript program to find the sum of the
// series 1 + 1/3 + 1/5 + ...
 
 
// Function to find the sum of the
// given series
function printSumSeries( N)
{
    // Initialise the sum to 0
    let sum = 0;
 
    for (let 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
   document.write(sum.toFixed(5));
}
 
// Driver Code
 
    let N = 6;
 
    printSumSeries(N);
     
// This code is contributed by todaysgaurav
 
 
</script>

                    

Output: 
1.87821

 

Time Complexity: O(N)

Auxiliary Space: O(1), since no extra space has been taken.



Last Updated : 19 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads