/* Hail.java * * This program generates a Hailstone series based on user input, * determines number of items in that series, and finally displays * the largest value seen. * */ import tio.*; public class Hail { public static void main(String args[]) { long hailval=0, // hailstone value holder count=0, // counter largest=0, // holds largest seen so far init=0; // remembers initial value System.out.println("Welcome to the Hailstone program."); System.out.print("Please enter the initial value: "); init = Console.in.readInt(); while (init <= 0) { System.out.print("Please enter a positive number: "); init = Console.in.readInt(); } count = 1; // we've seen one number largest = init; // it is the largest so far hailval = init; // and we begin with the initial value while ( hailval != 4 && hailval !=2 && hailval != 1) { // get next value if (hailval % 2 == 0) hailval /= 2; else hailval = hailval*3 + 1; // increment counter count++; // check for largest if (hailval > largest) largest = hailval; // uncomment to see sequence on screen // System.out.println(hailval); }//end while System.out.println("For the sequence starting at: " + init); System.out.print("There were " + count + " items and "); System.out.println(largest + " was the largest."); }//end main() }//end class // end Hail.java /* SAMPLE OUTPUT * $ java Hail Welcome to the Hailstone program. Please enter the initial value: 17 For the sequence starting at: 17 There were 11 items and 52 was the largest. $ java Hail Welcome to the Hailstone program. Please enter the initial value: 101 For the sequence starting at: 101 There were 24 items and 304 was the largest. $ java Hail Welcome to the Hailstone program. Please enter the initial value: 531 For the sequence starting at: 531 There were 122 items and 9232 was the largest. $ */