Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
457 B
1
Indexable
Never
import java.util.Scanner;

public class HarmonicSeriesSum {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the value of n: ");
        int n = scanner.nextInt();
        
        double sum = 0.0;
        for (int i = 1; i <= n; i++) {
            sum += 1.0 / i;
        }
        
        System.out.println("Sum of the harmonic series for n = " + n + " is: " + sum);
    }
}
Leave a Comment