This is a beta version of Practice-It. Give us feedback

logo Practice-It logo

printSequenceTo

Language/Type: Java method basics return while loops
Author: Stuart Reges (on 2020/10/06)

Write a static method called printSequenceTo that takes a target value as a parameter and that prints terms from a particular numerical sequence until they add up to a value greater than or equal to the target and that returns the number of terms that were included. For example, if the following calls are made:

                int n1 = printSequenceTo(3.0);
                int n2 = printSequenceTo(5.5);
            

The following output should be produced:

                1/2 + 2/3 + 3/4 + 4/5 + 5/6 = 3.5500000000000003
                1/2 + 2/3 + 3/4 + 4/5 + 5/6 + 6/7 + 7/8 + 8/9 = 6.171031746031746
            

The variable n1 is set to 5 because it took 5 terms from the sequence to get a sum that is at least 3.0. The variable n2 would be set to 8 because it took 8 terms to get a sum that is at least 5.5. You are to exactly reproduce the format of this output. You may assume that the target is greater than 0. Notice that the sum is not rounded.

Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.