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
120 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
43 Likes
Related Questions
Define a class to accept a number from user and check if it is an EvenPal number or not.
(The number is said to be EvenPal number when number is palindrome number (a number is palindrome if it is equal to its reverse) and sum of its digits is an even number.)
Example: 121 – is a palindrome number
Sum of the digits – 1+2+1 = 4 which is an even numberTo 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++)