Java Program to Find Harmonic Series


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.

Examples

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

Example 1

Input: n = 4

Output: 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}$.

Example 2

Input: n = 7

Output − 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}$.

Algorithm

  • Initialize an integer ‘n’.

  • Use loop and print $\frac{1}{i}$ until the i value is not equal to n.

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 for-loop and find the Harmonic series in Java.

The for loop is an iterative statement in java which executes the code until the condition fails.

for (initialization; condition; updation) {
   // code 
}
  • initialization − We need to initialize the loop with a value and it is executed only once.

  • condition − We need to specify a condition which specifies how many times the loop will be executed. The loop will be executed until this condition is true.

  • updation − We need to specify the value by which the loop should be incremented. It updates the loop initialization value.

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 will discuss how to implement Java Program for finding the Harmonic Series using While-loop.

while(condition){
   //code
}

The code gets executed until the condition becomes false.

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: 10-Apr-2023

587 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements