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)
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
Related Questions
Write the program in Java to display the following pattern:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1Write Java program to find the sum of the given series:
1 + 1 / (1+2) + 1 / (1+2+3) + ………. + 1 / (1+2+3+…..+n)
Write the program in Java to display the following pattern:
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1Write Java program to find the sum of the given series:
1 + (1*2) + (1*2*3) + ………. + (1*2*3* …… * n)