Assignemnt #76 and Collatz Sequence
Code
///Name: Daniel Tiffany-Appleton
///Period: 7
///Program Name: Collatz Sequence
///File Name: CollatzSequence
///Date Finished: 1/12/16
import java.util.Scanner;
public class CollatzSequence
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
int number, times;
times = 0;
System.out.print( "Starting number: " );
number = keyboard.nextInt();
System.out.print( number );
while( number != 1 )
{
if( number % 2 == 0 )
{
number = number / 2;
System.out.print( " " + number );
}
else if( number % 2 == 1 )
{
number = number * 3 + 1;
System.out.print( " " + number );
}
times++;
}
System.out.println( " ");
System.out.println( "Terminated after " + times + " steps.");
}
}
Picture of the output