ICSE Class 10 Computer Applications Question 39 of 69

Iterative Constructs in Java — Question 39

Back to all questions
39
Question

Question 30(vi)

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

x+13+x+25+x+37+...to n terms\dfrac{x + 1}{3} + \dfrac{x + 2}{5} + \dfrac{x + 3}{7} + ...\text{to n terms}

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();
        System.out.print("Enter x: ");
        int x = in.nextInt();

        double sum = 0;
        for (int i = 1, j = 3; i <= n; i++, j += 2) {
            sum += (x + i) / (double)j;
        }
        System.out.println("Sum = " + sum);
    }
}
Output
BlueJ output of KboatSeries.java
Answer