Assignemnt #76 Collatz Sequence
Code
///Name: Joshua Pell
///Period: 6
///Project Name: Collatz Sequence
///File Name: collatzSequence.java
///Date: 3/1/2015
import java.util.Scanner;
public class collatzSequence
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner( System.in );
int number, steps = 1;
System.out.print( "Starting number: " );
number = keyboard.nextInt();
int total = number;
do
{
if ( total % 2 == 0 ) // if even
{
total = total/ 2 ;
System.out.println( steps + ". " + total);
}
else
{
total = total * 3 + 1 ;
System.out.println( steps + ". " + total);
}
steps++;
} while (total > 1 );
}
}
Picture of the output