Assignment #76 Collatz Sequence
Code
/// Name: Lacey Reese
/// Period: 5
/// Program Name: CollatzSequence
/// File Name: CollatzSequence.java
/// Date Finished 3/9/2015
import java.util.Scanner;
public class CollatzSquence
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
int n;
System.out.print( "Starting number: " );
n = keyboard.nextInt();
System.out.print( n );
while ( n != 1 )
{
if ( n % 2 == 0 )
{
n = n / 2;
}
else if ( n % 2 != 0 )
{
n = ( 3 * n ) + 1;
}
System.out.print( "\t" + n );
}
System.out.println();
}
}
Picture of the output
//