
/**
 * This program prints the 3N+1 sequence for a starting value specified by
 * the user.  Note that if the user enters zero or a negative number, the
 * program will go into an infinite loop.  If will also fail if the number
 * in the sequence becomes too large to represent in a 32-bit integer.
 */
public class ThreeN {
 
   public static void main(String[] args) {                
      
      int N;       // for computing terms in the sequence
      int counter; // for counting the terms
      
      System.out.print("Enter a positive number N to start the sequence: ");
      N = TextIO.getlnInt();
      
      counter = 0;
      while (N != 1) {
         if (N % 2 == 0)
            N = N / 2;
         else
            N = 3 * N + 1;
         System.out.println(N);
         counter = counter + 1;
      }
      
      System.out.println();
      System.out.println("There were " + counter + " terms in the sequence.");
      System.out.println();
                        
  }
 
}  // end of class ThreeN

