Assignemnt #72 Collatz Sequence
Code
/// Name: JJ Deng
/// Period: 6
/// Program Name: Collatz Sequence
/// File Name: CollatzSequence.java
/// Date Finished: 1/10/2016
import java.util.Scanner;
public class CollatzSequence
{
public static void main( String[] args)
{
Scanner keyboard = new Scanner(System.in);
int n;
System.out.print( "Starting Number: ");
n= keyboard.nextInt();
while ( n!=1 )
{
if (n % 2 == 0)
{
n= n/2;
System.out.print( n + " ");
}
else
{
n= 3*n + 1;
System.out.print( n + " ");
}
}
}
}
Picture of the output