In this tutorial, we are going to write a Java program to generate Harmonic series in Java Programming with practical program code and step-by-step full complete explanation.

Java program to generate Harmonic series


Java program to generate Harmonic series.

 
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

 
153
Output of Harmonic Series is 5.6109180