Find Sum of Harmonic Series 1 + 1/2 + 1/3 + 1/4 + 1/5 +……+ 1/n
Here you will get the example code of Sum of Harmonic Series in Java programming.
Sum of Harmonic Series Formula
The harmonic series is a mathematical series in which the terms are the reciprocals of the positive integers. It is written as 1 + 1/2 + 1/3 + 1/4 + 1/5 + … The harmonic series is an example of a divergent series, meaning that the sum of the terms does not have a fixed value, but rather increases without limit.
Series:- 1 + 1/2 + 1/3 + 1/4 + 1/5 + …
Sum of Harmonic Series Program in Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import java.util.Scanner; class harmonicSeries { static double sum(double k) { double i, sum=0; for(i=1;i<=k;i++) { sum=sum+(1/i); } return(sum); } public static void main(String args[]) { double res; int num; Scanner sc=new Scanner(System.in); System.out.print("\nEnter how many terms: = "); num=sc.nextInt(); res=sum(num); System.out.print("\nsum of series upto "+num+" terms is "); System.out.format("%.4f", res); } } |
Output
Check out our other Java Programming Examples