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
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
Related Questions
Rewrite the following do while program segment using for:
x = 10; y = 20; do { x++; y++; } while (x<=20); System.out.println(x * y );Write a program in Java to find the Fibonacci series within a range entered by the user.
Sample Input:
Enter the minimum value: 10
Enter the maximum value: 20Sample Output:
13How 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; }