Loops 10

Description

The purpose of this challenge is to use looping structures (WHILE, DO-WHILE). This challenge calculates and displays the hailstone numbers of the Collatz Conjecture.

Collatz Conjecture

Just for fun, you can watch this video on the Collatz Conjecture. The process goes as below:

  • Take any positive number
  • If the number is even, divide it by 2
  • If the number is odd, multiply it by 3, then add 1 … (3n + 1)
  • Repetitively apply this process with each result
  • The conjecture states that eventually all results will end at 1
  • Each number generated is called a hailstone number (because it goes up and down like hailstones, before eventually falling to the ground)

Requirements

  1. Declare three integers: start, number and largest
  2. Declare and initialize an integer, called hailstones initialized to 0;
  3. Make sure to glance at the Sample Interaction to get an idea of the program flow and the user prompts
  4. Using a DO-WHILE statement, write a loop that  performs input validation, asking the user to enter the starting number. This number must not be less than 1. Use the start variable to receive the user input. Display a message within the loop if the user enters a number less than 1.
  5. Set number to start. This is so that we can preserve the value of start for later use.
  6. Set largest to start. We are assuming that the largest number found so far is the starting number.
  7. Write the main loop, using a WHILE statement. Within this while statement, write the necessary constructs to perform the calculations on the hailstone numbers
    1. Run the loop as long as the value of number is greater than 1. The conjecture states that the calculations must stop as soon we get to 1.
    2. If number is even, divide it by 2
    3. If number is odd, multiply it by 3 and add 1. Basically, apply the formula 3n +1
    4. Display the value of number. We will be displaying the hailstone numbers continuously on a line.
    5. Keep track of the largest hailstone number found so far. If number is found to be greater than largest, then set largest to number. 
    6. Increment hailstones. This allows us to count how many hailstone numbers have been generated.
  8. Display messages on how many hailstone numbers were generated and what the largest number found was. (See Sample Interaction).
  9. See what happens when you enter a starting number of 27

DO NOT USE

You may not use any user-defined functions, or any other loops other than DO-WHILE and WHILE.

Sample Interaction / Output

Enter a starting number: 1
Starting number must be greater than 1

Enter a starting number: 0
Starting number must be greater than 1

Enter a starting number: -10
Starting number must be greater than 1

Enter a starting number: 10
5 16 8 4 2 1
10 generated 6 hailstone numbers
Largest number reached: 16

RUN IT AGAIN:

Enter a starting number: 25
76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
25 generated 23 hailstone numbers
Largest number reached: 88

LEGEND
PROGRAM OUTPUT
USER ENTRY
FROM INPUT

CATALOG ID: CPP-FLOW00010

Print Requirements