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)
Java
Java Nested for Loops
25 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
![BlueJ output of Write Java program to find the sum of the given series: (1/2) + (1/3) + (1/5) + (1/7) + (1/11) + ………. + (1/29) BlueJ output of Write Java program to find the sum of the given series: (1/2) + (1/3) + (1/5) + (1/7) + (1/11) + ………. + (1/29)](https://cdn1.knowledgeboat.com/img/apc9/chap_9-p14.jpg)
Answered By
13 Likes