next up previous contents
Next: Solution Up: Control StructureIntrinsics and Previous: Solution

 

Mathematical Magic

If you take a positive integer, halve it if it is even or triple it and add one if it is odd, and repeat, then the number will eventually become one.

Set up a loop containing a statement to read in a number (input terminated by zero) and to print out the sequence obtained from each input. The number 13 is considered to be very unlucky and if it is obtained as part of the sequence then execution should immediately terminate with an appropriate message.

Demonstrate that your program works by outputting the sequences generated by the following sets of numbers:

  1. 7
  2. 106, 46, 3, 0

Go back to Notes gif




next up previous contents
Next: Solution Up: Control StructureIntrinsics and Previous: Solution

©University of Liverpool, 1997
Thu May 29 10:11:26 BST 1997
Not for commercial use.