Recursive program to find the Sum of the series 1 - 1/2 + 1/3 - 1/4 ... 1/N - GeeksforGeeks

23 Apr, 2021
Given a positive integer N, the task is to find the sum of the series 1 – (1/2) + (1/3) – (1/4) +.... (1/N) using recursion.
Examples: 
Input: N = 3 Output: 0.8333333333333333 Explanation: 1 – (1/2) + (1/3) = 0.8333333333333333
Input: N = 4 Output: 0.5833333333333333 Explanation: 1- (1/2) + (1/3) – (1/4) = 0.5833333333333333
Approach: The idea is to form a recursive case and base case in order to solve this problem using recursion. Therefore:  
Base Case: The base case for this problem is when we have calculated the sum till the value of N in the denominator. Therefore, we stop the recursion when the counter variable ‘i’ reaches the value N.
Recursive Case: The recursive case for this problem is to find whether the denominator is an even or odd. This is because, clearly, for every odd number, the preceding sign is a ‘+’ and for every even number, the preceding sign is a ‘-‘. Therefore, we check whether the counter variable is an even or odd number and accordingly add or subtract the value. Then, the function is recursively called for the next value of the value ‘i’.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ program to find the value of// the given series#include<bits/stdc++.h>using namespace std; // Recursive program to find the// value of the given seriesfloat sumOfSeries(int i, int n, float s){    // Base case    if (i > n )        return s;      // Recursive case    else    {          // If we are currently looking        // at the even number        if (i % 2 == 0)            s -= (float)1 / i;          // If we are currently looking        // at the odd number        else            s+= (float)1 / i;        return sumOfSeries(i + 1, n, s);    }} // Driver codeint main(){          // cout<<sumOfSeries(1,3,0);    float sum = sumOfSeries(1, 3, 0);    printf("%f", sum);    return 0;}  // This code is contributed by Rohit_ranjan
// Java program to find the value of// the given seriesimport java.util.*; class GFG{ // Recursive program to find the// value of the given seriesstatic float sumOfSeries(int i, int n, float s){         // Base case    if (i > n)        return s;     // Recursive case    else    {                 // If we are currently looking        // at the even number        if (i % 2 == 0)            s -= (float)1 / i;         // If we are currently looking        // at the odd number        else            s += (float)1 / i;                     return sumOfSeries(i + 1, n, s);    }} // Driver codepublic static void main(String[] args){    float sum = sumOfSeries(1, 3, 0);         System.out.printf("%f", sum);}} // This code is contributed by Amit Katiyar
# Python program to find the value of# the given series # Recursive program to find the# value of the given seriesdef sumOfSeries(i, n, s) :     # Base case    if i>n :        return s     # Recursive case    else :         # If we are currently looking        # at the even number        if i % 2 == 0 :            s-= 1 / i         # If we are currently looking        # at the odd number        else :            s+= 1 / i        return sumOfSeries(i + 1, n, s) # Driver codeif __name__ == "__main__":    print(sumOfSeries(1, 3, 0))
// C# program to find the value of// the given seriesusing System;class GFG{ // Recursive program to find the// value of the given seriesstatic float sumOfSeries(int i, int n, float s){    // Base case    if (i > n )        return s;     // Recursive case    else    {         // If we are currently looking        // at the even number        if (i % 2 == 0)            s -= (float)1 / i;         // If we are currently looking        // at the odd number        else            s += (float)1 / i;        return sumOfSeries(i + 1, n, s);    }} // Driver codepublic static void Main(){    float sum = sumOfSeries(1, 3, 0);    Console.Write(sum);}} // This code is contributed by Code_Mech
<script> // Javascript program to find the// value of the given series // Recursive program to find the// value of the given seriesfunction sumOfSeries(i, n, s){         // Base case    if (i > n)        return s;     // Recursive case    else    {                 // If we are currently looking        // at the even number        if (i % 2 == 0)            s -= 1 / i;         // If we are currently looking        // at the odd number        else            s += 1 / i;                     return sumOfSeries(i + 1, n, s);    }} // Driver codelet sum = sumOfSeries(1, 3, 0); document.write(sum); // This code is contributed by sravan kumar G </script>
0.8333333333333333
 
Rohit_ranjan
Code_Mech
amit143katiyar
sravankumar8128
Recursion
Recursion
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Recursive Insertion Sort
Program to calculate Height and Depth of a node in a Binary Tree
Sum of natural numbers using recursion
Decimal to binary number using recursion
Recursively Reversing a linked list (A simple implementation)
Practice Questions for Recursion | Set 1
How will you print numbers from 1 to 100 without using loop?
Types of Recursions
Print all possible words from phone digits
Program to reverse a string (Iterative and Recursive)