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