/*
* File: Hailstone.java
* --------------------
* This program is a stub for the Hailstone problem, which computes
* Hailstone sequence described in Assignment #2.
*/
import acm.program.*;
public class Hailstone extends ConsoleProgram {
public void run() {
// You fill this in
int n = readInt("Enter a number: ");
int count = 0;
while (n != 1) {
if (n % 2 == 0) {
print(n + " is even, so I take half:");
n /= 2;
println(n);
} else {
print(n + " is odd, so I make 3n + 1:");
n = n * 3 + 1;
println(n);
}
count++;
}
println("The process took " + count + " to reach 1");
}
}
* File: Hailstone.java
* --------------------
* This program is a stub for the Hailstone problem, which computes
* Hailstone sequence described in Assignment #2.
*/
import acm.program.*;
public class Hailstone extends ConsoleProgram {
public void run() {
// You fill this in
int n = readInt("Enter a number: ");
int count = 0;
while (n != 1) {
if (n % 2 == 0) {
print(n + " is even, so I take half:");
n /= 2;
println(n);
} else {
print(n + " is odd, so I make 3n + 1:");
n = n * 3 + 1;
println(n);
}
count++;
}
println("The process took " + count + " to reach 1");
}
}