Pin It

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 : 


 
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot