Find Harmonic Series in Java



The reciprocals of an arithmetic series without considering 0 is known as Harmonic series. If $a_{1},a_{2},a_{3}$? is arithmetic series then $\frac{1}{a1}$,$\frac{1}{a2}$,$\frac{1}{a3}$,? is the harmonic series. In this article, we will discuss how to implement a Java program to find the harmonic series.

Harmonic Series

For 1st term n = 1 and for every term n is incremented by 1 and at last the nth term the value is ?n'.

Harmonic series : $1+\frac{1}{2}+\frac{1}{3}$.......+$\frac{1}{n}$ Where, 1/n is the nth term of harmonic series.

Problem Statement

Write a Java program to find the Harmonic series. Below is a demonstration of the same ?

Harmonic Series: $\frac{1}{1}+\frac{1}{2}+\frac{1}{3}+\frac{1}{4}+.............\frac{1}{n}$

Input 1 

n = 4

Output 2

Harmonic Series till n is: $\frac{1}{1}+\frac{1}{2}+\frac{1}{3}+\frac{1}{4}$

In the above example, as the given input is 4, the series will start from $\frac{1}{1}$ and will print until $\frac{1}{4}$.

Input 2

n = 7

Output 2 

Harmonic Series till n are:$\frac{1}{1}+\frac{1}{2}+\frac{1}{3}+\frac{1}{4}+\frac{1}{5}+\frac{1}{6}+\frac{1}{7}$

In the above example, as the given input is 7, the series will start from 1/1 and will print until $\frac{1}{7}$.

Different approaches

In this article, we will discuss the different ways to find the harmonic series using Java Program.

Approach 1: Using for loop

In this approach, we will use a for-loop and find the Harmonic series in Java.

  • Import all classes from the java.util package.
  • Create a class named Main.
  • Inside the main method, initialize an integer variable n with a value (e.g., 10).
  • Print the message indicating the start of the Harmonic Series.
  • Use a for loop to iterate from 1 to n.
  • Inside the loop, print 1/i and check if i is not equal to n. If so, print " + " to separate the terms.
  • After exiting the loop, print a new line to finish the output.

Example 

In this example, we initialise a variable ?n' with an integer value and we iterate over the variable and print the 1/value every time. Once, the condition in for-loop is failed we come out of the loop.

import java.util.*;
public class Main {
   public static void main(String[] args) {
      int n = 10; 
      System.out.print("Harmonic Series up to "+ n + " terms: ");
      for (int i = 1; i <= n; i++) {
         System.out.print("1/" + i);
         if (i != n) {
            System.out.print(" + ");
         }
      }
      System.out.println();
   }
}

Output

Harmonic Series up to 10 terms: 1/1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7 + 1/8 + 1/9 + 1/10

Time Complexity: O(N)

Auxiliary Space: O(1)

Approach 2: Using while loop

In this approach we'll talk about how to implement Java Program for finding the Harmonic Series using While-loop.

  • Import all classes from the java.util package.
  • Create a class named Main.
  • Inside the main method, initialize an integer variable n with a value (e.g., 10).
  • Initialize another integer variable i to 1 for the loop iteration.
  • Print the message indicating the start of the Harmonic Series.
  • Use a while loop to iterate while i is less than or equal to n. and inside the loop, print 1/i and check if i is not equal to n. If so, print " + " to separate the terms.
  • Increment i by 1 after each iteration.
  • After exiting the loop, print a newline to finish the output.

Example 

In this example, we initialise a variable 'n' with an integer value, we also initialize another variable ?i' for iterating using while loop and we iterate over the variable and print the $\frac{1}{i}$ and increment the value of ?i' by 1 every time. Once, the condition in while-loop is failed we come out of the loop.

import java.util.*;
public class Main {
   public static void main(String[] args) {
      int n = 10;
      int i = 1;
      System.out.print("Harmonic Series up to "+ n + " terms: ");
      while (i <= n) {
         System.out.print("1/" + i);
         if (i != n) {
            System.out.print(" + ");
         }
         i++;
      }
      System.out.println();
   }
}

Output

Harmonic Series up to 10 terms: 1/1 + 1/2 + 1/3 + 1/4+ 1/5+ 1/6 + 1/7+ 1/8+ 1/9+ 1/10

Time Complexity: O(N)

Auxiliary Space: O(1)

In this article we have discussed the different approaches of finding Harmonic Series using Java program.

Updated on: 2025-02-26T16:48:17+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements