- Home
- Java Series Programs
Write Java program to find the sum of the given series:
Java Series Programs
Write Java program to find the sum of the given series:
(1/2) + (1/3) + (1/5) + (1/7) + (1/11) + ………. + (1/29)
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);
}
}