107k views
5 votes
Write a method to compute the following series

m(i) = 1/2 + 2/3 +......+ i/ (i+1)
Write a test program that displays the following table:
i m(i)
1 0.5000
2 1.1667
...
19 16.4023
20 17.3546
Class Name: Exercise06_13

User MorphicPro
by
7.7k points

1 Answer

4 votes

Final answer:

To compute the given series, we can use a loop to iterate through the numbers and add the terms to a sum variable.

Step-by-step explanation:

To write a method to compute the given series, we can use a loop to iterate through the numbers from 1 to i. For each iteration, we can add the term i / (i+1) to a sum variable. Here's the code for the method:



public static double computeSeries(int i) {
double sum = 0;
for (int j = 1; j <= i; j++) {
sum += (double) j / (j + 1);
}
return sum;
}



We can then write a test program to display the table by calling the method for each value of i and printing the result:



public static void main(String[] args) {
System.out.println("i\tm(i)");
for (int i = 1; i <= 20; i++) {
System.out.printf("%d\t%.4f\\", i, computeSeries(i));
}
}

User Irmantas
by
8.9k points

Related questions

asked Nov 1, 2024 176k views
Vanina asked Nov 1, 2024
by Vanina
8.2k points
1 answer
4 votes
176k views
asked Apr 12, 2020 171k views
Kiryl Plyashkevich asked Apr 12, 2020
by Kiryl Plyashkevich
7.7k points
1 answer
0 votes
171k views