Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to find sum of N number of terms in a Harmonic Progression in Java ?.

Program to demonstrate how to find sum of N number of terms in a Harmonic Progression in Java

package com.hubberspot.example;

import java.util.Scanner;

public class HarmonicProgression { 
   public static void main(String[] args) { 

     // Create a Scanner Object to obtain
     // User Input from console
     Scanner scanner = new Scanner(System.in);
     
      System.out.println("Enter the No. of terms in series : ");
      // Give the total no. of terms in series in int
      int noOfTerms = scanner.nextInt();
        
      // initial sum assigned to be zero
      double sum = 0.0;
        
     // applying the formula 
     // as : compute (1/1 + 1/2 + 1/3 + ... + 1/N)        
     for (int a = 1; a <= noOfTerms; a++) {
         sum = sum + 1.0 / a;
     }

     // printing out the Harmonic Sum of series
     System.out.println("Sum of Harmonic Series is : ");
     System.out.println(sum);
    }

}


Output of the program : 


 
 
© 2021 Learn Java by Examples Template by Hubberspot