KnowledgeBoat Logo

Computer Applications

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

S = 1 + (1*2) + (1*2*3) + ……. + (1*2*3* ……. * n)

Java

Java Iterative Stmts

21 Likes

Answer

import java.util.Scanner;

public class KboatSeries
{
    public void computeSeriesSum() {

        Scanner in = new Scanner(System.in);
        System.out.print("Enter n: ");
        int n = in.nextInt();
        long sum = 0, term = 1;
        
        for (int i = 1; i <= n; i++) {
            term *= i;
            sum += term;
        }
        
        System.out.println("Sum=" + sum);
    }
}

Variable Description Table

Program Explanation

Output

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

Answered By

8 Likes


Related Questions