KnowledgeBoat Logo

Java Series Programs

Write the program to find the sum of the following series:

S = a + a2 + a3 + ……. + an

Java

Java Iterative Stmts

ICSE

31 Likes

Answer

import java.util.Scanner;

public class KboatSeries
{
    public void computeSeriesSum() {

        Scanner in = new Scanner(System.in);
        System.out.print("Enter a: ");
        int a = in.nextInt();
        System.out.print("Enter n: ");
        int n = in.nextInt();
        int sum = 0;

        for (int i = 1; i <= n; i++) {
            sum += Math.pow(a, i);
        }
        
        System.out.println("Sum=" + sum);
    }
}

Output

BlueJ output of Write the program to find the sum of the following series: (a) S = a + a 2 + a 3 + ……. + a n

Answered By

12 Likes