Computer Applications
Write a program in Java to find the sum of the given series:
1 + 3/(1+2+3) + 5/(1+2+3+4+5) + …. to n terms
Java
Java Iterative Stmts
1 Like
Answer
import java.util.Scanner;
public class KboatSeries
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter n: ");
int n = in.nextInt();
double sum = 0;
double denom = 0;
for (int i = 1; i < 2*n; i += 2) {
denom += i + i - 1;
double t = i / denom;
sum += t;
}
System.out.println("Sum = " + sum);
}
}
Output

Answered By
3 Likes
Related Questions
How many times will the following loop execute? Write the output of the code:
int x=10; while (true){ System.out.println(x++ * 2); if(x%3==0) break; }
How many times will the following loop execute? Write the output of the code:
for(int j = 12; j >= 2; j -= 2) { if(j % 5 == 0) continue; System.out.println(j); }
Give the output of the following program segment. How many times is the loop executed?
for(x=10; x>20;x++) System.out.println(x); System.out.println(x*2);
To execute a loop 10 times, which of the following is correct?
- for (int i=11;i<=30;i+=2)
- for (int i=11;i<=30;i+=3)
- for (int i=11;i<20;i++)
- for (int i=11;i<=21;i++)