Untitled
unknown
plain_text
2 years ago
576 B
8
Indexable
import java.util.Scanner;
public class HarmonicSeriesSum {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input value of n
System.out.print("Enter the value of n: ");
int n = scanner.nextInt();
// Calculate the sum of the harmonic series
double sum = 0.0;
for (int i = 1; i <= n; i++) {
sum += 1.0 / i;
}
// Display the sum
System.out.println("Sum of the harmonic series up to " + n + " terms: " + sum);
scanner.close();
}
}Editor is loading...
Leave a Comment