C++ Program to Find the Sum of harmonic Series

Finding the sum of the harmonic series, which is expressed as

sum = 1 + \frac{1}{2} + \frac{1}{3} + \ldots + \frac{1}{n}

Finding the series is a common mathematical problem that can be implemented easily using C++. In this article, we will provide an in-depth explanation of the harmonic series, the prerequisites for understanding and implementing it, and present three different solutions with detailed explanations and outputs. Let’s dive into the world of harmonic series in C++.

Introduction

The harmonic series is the sum of reciprocals of the first nnn natural numbers. It is a classic problem in mathematics and computer science, often encountered in various contexts such as number theory, analysis, and algorithm design.

Definition

The harmonic series for a given nnn is defined as:

H_n = 1 + \frac{1}{2} + \frac{1}{3} + \ldots + \frac{1}{n}

where Hn​ represents the nth harmonic number.

Prerequisites

Before we begin with the implementations, ensure you have the following:

  • Basic understanding of C++ programming, including loops and functions.
  • Familiarity with floating-point arithmetic in C++.
  • A C++ compiler to compile and run the programs.

Example Implementations

Solution 1: Iterative Approach to Find Sum of Harmonic Series

Explanation

In this solution, we will use a simple iterative approach to compute the sum of the harmonic series. We will loop from 1 to nnn, adding the reciprocal of each number to the sum.

Code

C++
#include <iostream>
using namespace std;

double harmonicSum(int n) {
    double sum = 0.0;
    for (int i = 1; i <= n; ++i) {
        sum += 1.0 / i;
    }
    return sum;
}

int main() {
    int n;
    cout << "Enter a positive integer: ";
    cin >> n;

    double result = harmonicSum(n);
    cout << "The sum of the series 1 + 1/2 + 1/3 + ... + 1/" << n << " is: " << result << endl;

    return 0;
}

Output

C++
Enter a positive integer: 5
The sum of the series 1 + 1/2 + 1/3 + ... + 1/5 is: 2.28333

Solution 2: Recursive Approach to Find Sum of Harmonic Series

Explanation

In this solution, we will use recursion to compute the harmonic sum. This approach involves a function that calls itself with a reduced value of nnn until the base case of n=1n = 1n=1 is reached.

Code

C++
#include <iostream>
using namespace std;

double harmonicSumRecursive(int n) {
    if (n == 1)
        return 1.0;
    else
        return 1.0 / n + harmonicSumRecursive(n - 1);
}

int main() {
    int n;
    cout << "Enter a positive integer: ";
    cin >> n;

    double result = harmonicSumRecursive(n);
    cout << "The sum of the series 1 + 1/2 + 1/3 + ... + 1/" << n << " is: " << result << endl;

    return 0;
}

Output

C++
Enter a positive integer: 5
The sum of the series 1 + 1/2 + 1/3 + ... + 1/5 is: 2.28333

Solution 3: Using STL Algorithms to Find Sum of Harmonic Series in C++

Explanation

In this solution, we will leverage the Standard Template Library (STL) to compute the harmonic sum. We will use the accumulate function along with a lambda function to achieve this.

Code

C++
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;

double harmonicSumSTL(int n) {
    vector<int> v(n);
    iota(v.begin(), v.end(), 1); // Fill the vector with values from 1 to n
    return accumulate(v.begin(), v.end(), 0.0, [](double sum, int i) {
        return sum + 1.0 / i;
    });
}

int main() {
    int n;
    cout << "Enter a positive integer: ";
    cin >> n;

    double result = harmonicSumSTL(n);
    cout << "The sum of the series 1 + 1/2 + 1/3 + ... + 1/" << n << " is: " << result << endl;

    return 0;
}

Output

C++
Enter a positive integer: 5
The sum of the series 1 + 1/2 + 1/3 + ... + 1/5 is: 2.28333

Conclusion

In this article, we explored the concept of the harmonic series and provided three differentC++ Program to Find the Sum of harmonic Series . We used an iterative approach, a recursive approach, and an approach leveraging the STL. Each method has its unique advantages and showcases different aspects of C++ programming