Computer Applications
Write a program in Java to find the sum of the given series:
x - x2/3 + x3/5 - x4/7 + …. to n terms
Java
Java Iterative Stmts
117 Likes
Answer
import java.util.Scanner;
public class KboatSeries
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter x: ");
int x = in.nextInt();
System.out.print("Enter n: ");
int n = in.nextInt();
double sum = 0;
int a = 1;
for (int i = 1, j = 1; i <= n; i++, j+=2) {
double term = Math.pow(x, i) / j * a;
sum += term;
a *= -1;
}
System.out.println("Sum = " + sum);
}
}
Variable Description Table
Program Explanation
Output

Answered By
41 Likes
Related Questions
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++)
Which of the following are entry controlled loops?
(a) for
(b) while
(c) do..while
(d) switch
- only a
- a and b
- a and c
- c and d
To execute a loop 5 times, which of the following is correct?
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); }