Sum of the Series 1 + 1/2 + … + 1/n: flowchart, algorithm and program

This program reads n and adds up the fractions 1/1, 1/2, 1/3, … up to 1/n, then prints the total as a decimal. A loop runs i from 1 to n, and each time the value 1.0 / i is added to a running total called sum.

▶ Watch the dry run step by step →

Algorithm

  1. Step 1: Start.
  2. Step 2: Set sum = 0.
  3. Step 3: Read n.
  4. Step 4: Set i = 1.
  5. Step 5: If i <= n, go to step 6; otherwise go to step 8.
  6. Step 6: Set sum = sum + 1.0 / i.
  7. Step 7: Set i = i + 1. Go to step 5.
  8. Step 8: Print sum.
  9. Step 9: Stop.

Pseudocode

BEGIN
    sum ← 0
    READ n
    i ← 1
    WHILE i <= n DO
        sum ← sum + 1.0 / i
        i ← i + 1
    END WHILE
    PRINT sum
END

Program in C, Python and Java

Sum of the Series 1 + 1/2 + … + 1/n program in C

#include <stdio.h>

int main() {
    int n, i;
    double sum = 0;
    printf("Enter n: ");
    scanf("%d", &n);
    for (i = 1; i <= n; i++) {
        sum = sum + 1.0 / i;
    }
    printf("Sum = %.4f\n", sum);
    return 0;
}

Sum of the Series 1 + 1/2 + … + 1/n program in Python

n = int(input("Enter n: "))
total = 0.0
for i in range(1, n + 1):
    total = total + 1.0 / i
print("Sum = %.4f" % total)

Sum of the Series 1 + 1/2 + … + 1/n program in Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter n: ");
        int n = sc.nextInt();
        double sum = 0;
        for (int i = 1; i <= n; i++) {
            sum = sum + 1.0 / i;
        }
        System.out.printf("Sum = %.4f%n", sum);
    }
}

Sample input: n = 4
Output: Sum = 2.0833

How it works

  • sum starts at 0 and is a double, so it can hold decimal values like 0.5 and 0.3333.
  • The for loop runs i = 1, 2, 3, … n. In every round it adds one term, 1.0 / i, to sum.
  • Writing 1.0 instead of 1 makes the division use decimals. With 1 / i both numbers are int, so 1/2 would become 0.
  • For n = 4 the sum grows like this: 1, 1.5, 1.8333, 2.0833. When i becomes 5 the condition i <= n is false and the loop stops.
  • printf with %.4f prints sum rounded to 4 digits after the decimal point, so you see 2.0833.

Dry run (trace table)

StepLineBoxsumniOutput
13Start
25sum = 00.0
37Read n0.04Enter n:
48i = 10.041
58i <= n?0.041
69sum = sum + 1.0 / i1.041
78i = i + 11.042
88i <= n?1.042
99sum = sum + 1.0 / i1.542
108i = i + 11.543
118i <= n?1.543
129sum = sum + 1.0 / i1.833343
138i = i + 11.833344
148i <= n?1.833344
159sum = sum + 1.0 / i2.083344
168i = i + 12.083345
178i <= n?2.083345
1811Print sum2.083345Sum = 2.0833
1912Stop2.083345

Time and space complexity

Time: O(n) · Space: O(1). The loop runs n times doing constant work each time, using only a few variables.

Common mistakes

  • Writing 1 / i instead of 1.0 / i. Integer division gives 0 for every i greater than 1, so the sum stays at 1.
  • Declaring sum as int, which throws away the fractional part on every addition.
  • Starting the loop at i = 0, which divides by zero, or using i < n, which leaves out the last term 1/n.

Viva questions

Q1. Why is sum initialised to 0 and not 1?

Sum is an accumulator, and 0 is the identity for addition, so it does not change the result. If it started at 1, the answer would be one too large, because the loop already adds the 1/1 term.

Q2. Why do you write 1.0 / i instead of 1 / i?

If both operands are int, C does integer division, so 1/2 gives 0 and every term after the first would be 0. Using 1.0 makes it a double division, so the real fraction is kept.

Q3. Why is sum declared as double and not int or float?

The terms are fractions, so the sum needs a type that can hold decimals, and an int would lose them. A double has more precision than a float, so less rounding error builds up over many terms.

Q4. What does the condition i <= n check, and why start i at 1?

It keeps the loop running until the last term 1/n has been added, so n is included. i starts at 1 because the first term is 1/1, and starting at 0 would divide by zero.

Q5. What does the program print if n is 0 or negative?

The loop condition 1 <= n is false from the start, so the body never runs. The sum stays 0 and it prints Sum = 0.0000. The program does not reject the input, and you could add a check to ask for a positive n.

Q6. What is the time complexity of this program, and the space complexity?

Time is O(n), because the loop runs n times and does a constant amount of work each time. Space is O(1), because it only uses a few variables.

Q7. What is this series called, and does the sum keep growing as n increases?

It is the harmonic series, and its partial sum is the n-th harmonic number. The sum has no upper limit, but it grows very slowly, roughly like ln(n).

Q8. What would change if you wanted the series 1 + 1/2² + 1/3² + … + 1/n²?

Only the term in the loop changes, to sum = sum + 1.0 / (i * i). Unlike the harmonic series, this sum converges, towards π²/6, which is about 1.645.

Q9. What does %.4f do in the printf, and does it change the value stored in sum?

It prints the double rounded to 4 digits after the decimal point. It only affects how the value is displayed, and sum keeps its full precision.

Frequently asked questions

Why do we use 1.0 / i and not 1 / i in C?

In C, dividing two ints gives an int, so 1 / 2 is 0. Using 1.0 makes it a double division, so 1.0 / 2 correctly gives 0.5.

What is the sum of 1 + 1/2 + 1/3 + 1/4?

It is 1 + 0.5 + 0.3333 + 0.25 = 2.0833 (to 4 decimal places), which is the sample output for n = 4.

What is this series called, and does it ever stop growing?

It is called the harmonic series. Its partial sums keep growing as n increases, but very slowly, and the sum never settles at a fixed value.

More programs

← All programs

Try AI Flowchart free →