Assignemnt #76 and Collatz Sequence
Code
/// Name: David Shkolnikov
/// Period: 6
/// Program Name: Collatz Sequence
/// File Name: CollatzSequence.java
/// Date Finished: 1/27/2015
import java.util.Scanner;
public class CollatzSequence
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
int n, count = 0;
System.out.print("Starting number: ");
n = keyboard.nextInt();
while ( n != 1 )
{
System.out.print(n + "\t");
if (n % 2 == 0)
n = n / 2;
else
n = (3 * n) + 1;
count++;
}
System.out.println(n);
System.out.println("\nTerminated after " + count + " steps.");
}
}
Picture of the output