Question: For this lab you will write a Java program that prompts the user to enter a positive integer. Your program should use a loop to
For this lab you will write a Java program that prompts the user to enter a positive integer. Your program should use a loop to compute a Hailstone Series, using that integer as a start point. The Hailstone series is defined as follows: start with any integer n strictly greater than 0. If n is even, then the next value in the series is n/2. If n is odd, then the next value in the series is 3*n + 1.
This is a sample transcript of what your program should do. Items in bold are user input and should not be put on the screen by your program. Enter a starting value: 19 19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 A second run of the code might look like this: Enter a starting value: 18 18, 9, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 Note that when the series reaches the value of 1, the sequence will start repeating: 1, 4, 2, 1 ... At this point we say that the series has converged and our loop should stop rather than repeat infinitely. Make sure your loop stops when it reaches the value of 1.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
