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
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