Computer Applications
Write a program in Java to find the sum of the given series:
x1 - x2 + x3 - x4 … - xn , where x = 3
Java
Java Iterative Stmts
9 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 n: ");
int n = in.nextInt();
int x = 3;
double sum = 0;
for (int i = 1; i <= n; i++) {
double term = Math.pow(x, i);
if (i % 2 == 0)
sum -= term;
else
sum += term;
}
System.out.println("Sum = " + sum);
}
}
Output

Answered By
3 Likes
Related Questions
Write a menu driven program to access a number from the user and check whether it is a BUZZ number or to accept any two numbers and to print the GCD of them.
- A BUZZ number is the number which either ends with 7 or is divisible by 7.
- GCD (Greatest Common Divisor) of two integers is calculated by continued division method. Divide the larger number by the smaller; the remainder then divides the previous divisor. The process is repeated till the remainder is zero. The divisor then results the GCD.
Write a program in Java to find the sum of the given series:
x1 + x2 + x3 + x4 … + xn
Write a program in Java to find the sum of the given series:
1/x1 + 2/x2 + 3/x3 + … + n/xn
Write a program in Java to find the sum of the given series:
1/2 + 2/3 + 3/4 + … + 49/50