KnowledgeBoat Logo

Java Series Programs

Write a program in Java to input the values of x and n and print the sum of the following series:

S = 1 + (x+2)/2! + (2x+3)/3! + (3x+4)/4! + ……… to n terms

Java

Java Nested for Loops

ICSE

35 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 x: ");
        int x = in.nextInt();
        System.out.print("Enter n: ");
        int n = in.nextInt();
        double sum = 1;
        for (int i = 2; i <= n; i++) {
            double f = 1;
            for (int j = 1; j <= i; j++) {
                f *= j;
            }
            double t = ((i - 1) * x + i) / f;
            sum += t;
        }
        System.out.println("Sum = " + sum);
    }
}

Output

BlueJ output of Write a program in Java to input the values of x and n and print the sum of the following series: S = 1 + (x+2)/2! + (2x+3)/3! + (3x+4)/4! + ……… to n terms

Answered By

14 Likes