Java program to find out the sum of given H.P. Series.




Harmonic progression (H.P.):
Harmonic progression is a progression formed by taking the reciprocals of an arithmetic progression.


Example of H.P. series:
1/3, 1/6, 1/9, …
If you take the reciprocal of each term from the above HP, the sequence will become
3, 6, 9, ...
which is an AP with a common difference of 3.



import java.util.Scanner;
public class Series
{
 public static void main(String args[]) 
 {
  Scanner sc = new Scanner(System.in);
  int n;
  float sum, t;
  System.out.println("1+1/2+1/3+......+1/n");
  System.out.println("Enter the value of n");
  n = sc.nextInt();
  sum = 0;
  for (float i = 1; i <= n; i++) 
  {
   t = 1 / i;
   sum = sum + t;
  }
  System.out.println(sum);
 }
}

Output:
1+1/2+1/3+......+1/n
Enter the value of n
5
2.283334
BUILD SUCCESSFUL (total time: 6 seconds)



No comments:

Post a Comment