Assignment #76

Code

    ///Name: Cody Swain
    ///Period: 6
    ///Project Name: CollatzSequence
    ///File Name: CollatzSequence.java
    ///Date: 1/8/16
    
    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 );
        }
    }
        
        
                    
                
        
    

Picture of the output

Assignment 76