Java program to find sum of harmonic series 1 + 1/2 + 1/3 + 1/4 + 1/5 +……+ 1/n

Java program to find sum of harmonic series 1 + 1/2 + 1/3 + 1/4 + 1/5 +......+ 1/n

class Harmonic
{
public static void main(String…s)
{
int n,i;
float sum=0;

n=Integer.parseInt(s[0]);

for(i=1;i<=n;i++)
{
sum=sum+(float)1/i;
}

System.out.println(“nSum=”+sum);
}
}

2 thoughts on “Java program to find sum of harmonic series 1 + 1/2 + 1/3 + 1/4 + 1/5 +……+ 1/n

  1. deepa

    class Harmonic
    {
    public static void main(String…s)
    {
    int n,i;
    float sum=0;

    n=Integer.parseInt(s[0]);

    for(i=1;i<=n;i++)
    {
    sum=sum+(float)1/i;
    }

    System.out.println(“nSum=”+sum);
    }
    }

    Reply
  2. Jatin Singh

    Plz tell the code for harmonic series
    S = 1-1/2 + 1/3 – 1/4…… -1/10

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *