KnowledgeBoat Logo

Java Series Programs

Write a program to compute and display the sum of the following series:

S = (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + -------- + (1 + 2 + 3 + ----- + n ) / (1 * 2 * 3 * ----- * n)

Java

Java Nested for Loops

ICSE 2007

139 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();
        double sum = 0.0;
        for (int i = 2; i <= n; i++) {
            double num = 0.0, den = 1.0;
            for (int j = 1; j <= i; j++) {
                num += j;
                den *= j;
            }
            sum = sum + (num / den);
        }
        System.out.println("Sum=" + sum);
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a program to compute and display the sum of the following series: S = (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + -------- + (1 + 2 + 3 + ----- + n ) / (1 * 2 * 3 * ----- * n)

Answered By

56 Likes