ICSE Class 10 Computer Applications Question 42 of 69

Iterative Constructs in Java — Question 42

Back to all questions
42
Question

Question 30(ix)

Write a program in Java to find the sum of the given series:

1 + 3 + 7 + 15 + 31 + ..... + (220 - 1)

public class KboatSeries
{
    public static void main(String args[]) {

        double sum = 0;
        
        for (int i = 1; i <= 20; i++) {
            double term = Math.pow(2,i) - 1;
            sum += term;
        }
            
        System.out.println("Sum = " + sum);
        
    }
}
Output
BlueJ output of KboatSeries.java
Answer