Computer Applications

Write Java program to find the sum of the given series:

(1/2) + (1/3) + (1/5) + (1/7) + (1/11) + ………. + (1/29)

Java

Java Nested for Loops

26 Likes

Answer

public class KboatSeriesSum
{
    public static void main(String args[]) {
        double sum = 0.0;
        for (int i = 2; i < 30; i++) {
            boolean isPrime = true;
            for (int j = 2; j <= i / 2; j++) {
                if (i % j == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime)
                sum += 1.0 / i;
        }
        System.out.println("Sum=" + sum);
    }
}

Variable Description Table

Program Explanation

Output

Answered By

14 Likes


Related Questions