Assignemnt #76 and Collatz Sequence
Code
/// Name: Kyle Ivy
/// Period: 5
/// Program Name: SafeSquareRoot
/// File Name: SafeSquareRoot.java
/// Date Finished: 1/5/2016
import java.util.Scanner;
public class CollatzSequence {
public static void main( String[] args ) {
Scanner keyboard = new Scanner(System.in);
int n, steps = 0;
System.out.print( "Starting Number for Collatz Sequence: " );
n = keyboard.nextInt();
System.out.print( n + "\t" );
while ( n != 1 ){
if ( n%2 == 0 ){
n = n/2;
System.out.print( n + "\t" );
steps++;
}
else if ( n%2 != 0 ){
n = (n*3)+1;
System.out.print( n + "\t" );
steps++;
}
}
System.out.println();
System.out.println();
System.out.println( "Totals numbers: " + steps );
}
}
prog76.PNG