// Project: Loops // Module: hailstone // Source code file: Main.java // Given the start value, generate the Hailstone sequence: // If n is even n = n / 2, otherwise n = 3 * n + 1. // The sequence is called a Hailstone sequence because its // values move up and down in an unpredictable manner, similar // to hailstones in a thunderstorm cloud. // Mathematicians believe that all Hailstone sequences terminate // with one, even though they have not been able to formally prove this // for all starting values. import java.util.Scanner; public class Main { public static void main(String[] args) { // Create Scanner object. Scanner input = new Scanner(System.in); // Display input prompt. System.err.print("Enter start value for Hailstone sequence: "); // Get start value. int startValue = input.nextInt( ); int counter = 1; for(int n = startValue; n > 1; ) { if (n % 2 == 0) { n /= 2; } else { n = 3 * n + 1; } counter++; System.out.print(n + " "); if (counter % 20 == 0) { System.out.println( ); } } // Close Scanner object. input.close( ); } }