Assignemnt #76 CollatzSequence
Code
/// Name: Jake Johnson
/// Period: 7
/// Program Name: CollatzSequence
/// File Name: CollatzSequence.java
/// Date Finished: 2/2/2016
import java.util.Scanner;
public class CollatzSequence {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int startNum, count;
count = 0;
System.out.print("Collatz Sequence! \nPlease enter the starting number: ");
startNum = keyboard.nextInt();
while (startNum!=1) {
if (startNum%2 == 0) {
startNum = startNum/2;
System.out.print(startNum + " ");
count++;
} else {
startNum = startNum*3+1;
System.out.print(startNum + " ");
count++;
}
}
System.out.println("\n\nThat took " + count + " steps.");
}
}
Picture of the output