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



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

Adam Marshall ©University of Liverpool, 1996
Fri Dec 6 14:10:26 GMT 1996