KnowledgeBoat Logo

Java Series Programs

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

S = 1 + (3/2!) + (5/3!) + (7/4!) + ……. to n

Java

Java Nested for Loops

ICSE

63 Likes

Answer

import java.util.Scanner;

public class KboatSeries
{
    public void computeSum() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter n: ");
        int n = in.nextInt();
        double sum = 0.0;
        for (int i = 1, j = 1; i <= n; i++, j = j + 2) {
            double f = 1;
            for (int k = 1; k <= i; k++) {
                f *= k;
            }
            sum += j / f;
        }
        System.out.println("Sum=" + sum);
    }
}

Output

BlueJ output of Write a program in Java to find the sum of the following series: S = 1 + (3/2!) + (5/3!) + (7/4!) + ……. to n

Answered By

30 Likes