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.
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 seriesvoid 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 Codeint 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 seriesdef 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 Codeif __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 seriesfunction 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)


