Harmonic Progression Sum with While Loop

This program calculates sum of harmonic progression using while loop.

Problem Statement

Write a Java program that calculates sum of harmonic progression up to 1/8 using while loop.

Source Code

1 public class HarmonicSumWhile {
2 public static void main(String[] args) {
3 int n = 8, i = 1;
4 double sum = 0.0;
5 while(i <= n) {
6 sum += 1.0/i;
7 i++;
8 }
9 System.out.println("Sum of harmonic progression = " + sum + " - Eduinq");
10 }
11 }

Program Output

Sum of harmonic progression = 2.7178571428571425 - Eduinq

Explanation

The program initializes i=1 and iterates until i<=n. Each iteration adds reciprocal of i to sum. This demonstrates how while loops can calculate harmonic progression sums. Shows how while loop can calculate harmonic progression sums.

Quick Links to Explore