- Home
- Chapter 1 - Object Oriented Programming Concepts
- Object Oriented Programming Concepts
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 2 - Introduction to Java
- Introduction to Java
- Multiple Choice Questions
- Assignment Questions
- Chapter 3 - Values and Data Types
- Values and Data Types
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 4 - Operators in Java
- Operators in Java
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 5 - User-Defined Methods
- User-Defined Methods
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 6 - Input in Java
- Input in Java
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 7 - Mathematical Library Methods
- Mathematical Library Methods
- Multiple Choice Questions
- Assignment Questions
- Chapter 8 - Conditional Constructs in Java
- Conditional Constructs in Java
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 9 - Iterative Constructs in Java
- Iterative Constructs in Java
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions and Programs
- Chapter 10 - Nested for loops
- Nested for loops
- Assignment Questions and Programs
- Chapter 11 - Constructors
- Constructors
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 12 - Library Classes
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 13 - Encapsulation and Inheritance
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 14 - Arrays
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 15 - String Handling
- Library Classes
- Multiple Choice Questions
- Assignment Questions
Iterative Constructs in Java
Chapter 9
Iterative Constructs in Java
Class 10 - Logix Kips ICSE Computer Applications with BlueJ
![]() Share with a Friend |
Java Program: Sum of Alternating Powers Series
30(ii). Write a program in Java to find the sum of the given series:
x1 - x2 + x3 - x4 + ..... - xn , where x = 3
Program Title: Sum of Alternating Powers Series
Here’s a Java program to compute the alternating series:
x1 − x2 + x3 − x4 + ⋯ ± xn
with x = 3.
public class AlternatingSeries {
public static void main(String[] args) {
int x = 3; // value of x
int n = 5; // number of terms, can be changed
int sign = 1; // to alternate + and -
double sum = 0.0;
for (int i = 1; i <= n; i++) {
sum += sign * Math.pow(x, i);
sign *= -1; // alternate the sign
}
System.out.println("Sum of the series for x = " + x + " and n = " + n + " is: " + sum);
}
}
Output
Sample Input / Output (n = 5) Sum of the series for x = 3 and n = 5 is: 177.0
📝 Explanation
31 – 32 + 33 – 34 + 35 = 3 – 9 + 27 – 81 + 243 = 177
How It Works
- sign variable starts at +1
- For each term, multiply the power of x with sign
- Flip the sign using sign *= -1 to alternate between + and -
- Sum all terms to get the result
