Java Program for Harmonic Series

A harmonic series is that in which reciprocal of series forms the Arithmetic Progression. 
The example for harmonic  series is : 1+(1/2)+(1/3)+(1/4)+..... which forms a harmonic series.

Java Program Code to generate Harmonic Series

/* Write a Java program to generate Harmonic Series.
Program by Codext.com
 */

class HarmonicSeries{

      public static void main(String args[]){

      int num = Integer.parseInt(args[0]);

      double result = 0.0;

      while(num > 0){

            result = result + (double) 1 / num;

            num--;

      }

      System.out.println("Output of Harmonic Series is "+result);

  }

}

Output of Harmonic Series Java Program


0 comments:

Post a Comment