Computer Applications

Write a program to read the number x using the Scanner class and compute the series:

Sum = x/2 + x/5 + x/8 + x/11+ ….. + x/20

The output should look like as shown below:

Enter the value of x: 10
Sum of the series is: 10.961611917494269

Java

Java Iterative Stmts

1 Like

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();
        double sum = 0.0;
        for (int i = 2; i <= 20; i += 3)
            sum += (double)x / i;
        System.out.println("Sum = " + sum);
    }
}

Output

Answered By

1 Like


Related Questions