Search This Blog

If you like any posts in this blog then click this icon which is present under the post

Monday 10 October 2011

Program for Hormonic series

Program
import java.io.*;
class HormonicSeries
{
    public static void main(String args[])
    {
        float s=0f;int n=0;
        try
        {

            DataInputStream din=new DataInputStream(System.in);
            System.out.println("Enter n value");
            n=Integer.parseInt(din.readLine());
            for(int i=1;i<=n;i++)
                s+=(float)1/i;
            s=System.out.println("sum="+s);
        }
        catch(IOException e)
        {
            System.out.println(e);
        }
    }
}
Output
Enter n value
10
sum=2.9289684

No comments:

Post a Comment

Followers