Assignment #76
Code
/// Name: Jonathan Stine
/// Period: 5
/// Program Name: Collatz Sequence
/// File Name: Collatz.java
/// Date Finished: 12/9/2015
import java.util.Scanner;
public class Collatz {
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();
}
}
Code Output