Write a script that prompts the user for an integer (in the range 1 to 10000) and reports?

to reach the value 1. The Collatz sequence is a sequence of positive integers obeying

the following:

• if a number in the sequence, n, is even, then the next number in the sequence is

n/2.

• if a number in the sequence, n, is odd, then the next number in the sequence is

3n + 1.

So, for example, if your user input 10, then your script should produce the value 6

because the sequence

10 → 5 → 16 → 8 → 4 → 2 → 1

1 Answer

Relevance
  • 1 decade ago
    Favorite Answer

    First, prompt the user.

    Then check whether the input is valid.

    After that, you need a loop that will run as long as n does not equal 1.

    Inside the loop you need a selection statement (e.g. "if"). Use n mod 2 to determine whether n is even or odd.

    After the selection statement but before the end of the loop, you output the value of n.

Still have questions? Get your answers by asking now.