Sum of Series (1 + 1/2 + 1/3...) in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

double sumSeries(int n) {
    double s = 0;
    for (int i = 1; i <= n; i++)
        s += 1.0 / i;
    return s;
}

C Output

Input:  
5  

Output:  
Sum = 2.283334


C++ Program

double sumSeries(int n) {
    double s = 0;
    for (int i = 1; i <= n; i++)
        s += 1.0 / i;
    return s;
}

C++ Output

Input:  
5  

Output:  
Sum = 2.28333


JAVA Program

double sumSeries(int n) {
    double s = 0;
    for (int i = 1; i <= n; i++)
        s += 1.0 / i;
    return s;
}

JAVA Output

Input:  
5 

Output:  
Sum = 2.283333333333333


Python Program

def sum_series(n):
    print("Sum =", sum(1 / i for i in range(1, n + 1)))

Python Output

Input:  
5  

Output:  
Sum = 2.283333333333333


In-Depth Explanation
Example
If n = 5, we compute:


1 + 1/2 + 1/3 + 1/4 + 1/5
= 1 + 0.5 + 0.333. + 0.25 + 0.2
= 2.283333.
This type of series is referred to as the harmonic series, and even though each term is getting smaller, the sum continues to rise slowly as n gets larger.

Real-Life Analogy
Imagine a pizza shared among individuals:

The first individual gets a complete pizza (1)

The second receives half (1/2)

The third receives a third of it, and so on.
Each receives decreasing amounts, but the overall amount of pizza eaten always grows — albeit extremely slowly. This is exactly how the harmonic series behaves: diminishing pieces, yet still summing up.

Why It Matters
The harmonic series occurs in:

Probability and statistics

Computer science algorithms (e.g., time complexity of specific loops)

Physics and engineering, particularly in waveforms and resonance

Data structures such as heaps and skip lists (average-case analysis)

It also familiarizes you with:

Floating-point arithmetic

Loop logic

Accuracy in mathematical computations

What You Learn from This
You know:

Floating or double precision operations

Summation of series with loops or comprehension

The fact that series can increase indefinitely even as terms diminish

Mathematical elegance of infinite, slowly increasing series
This lays a solid foundation for coping with:

Recursive series

Logarithmic growth
Approximation methods

Interview Relevance and Real Projects
This is usually used to probe:

Loop logic

Floating-point operations

Your knowledge of series convergence

Applications in real life are:

Estimating time complexity in algorithms such as insertion sort (average case)

Financial models that handle diminishing returns

Approximations of machine learning cost functions
The harmonic series (1 + 1/2 + 1/3 +. + 1/n) is a basic mathematical and computer science concept, applied to estimate runtime complexity, study growth patterns, and solve real-world summation problems. Applying this in C, C++, Java, and Python assists novices in grasping loop computations, precision management, and numerical patterns. It's asked frequently in programming interviews and contest coding problems to evaluate logical reasoning and problem-solving skills. Acquiring the harmonic series not only makes your programming loop basics and number manipulation solid but also opens doors to series behavior knowledge in scientific and algorithmic uses.