Assignemnt #76: Collatz Sequence
Code
/// Name: Sean Harrison
/// Period: 7
/// Program name: CollatzSequence
/// File Name: CollatzSequence.java
/// Date Finished: 1/29/2016
import java.util.Scanner;
public class CollatzSequence
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
int Number;
System.out.println( "What is your starting number? It must be greater than 0. " );
Number = keyboard.nextInt();
while ( Number <= 0 )
{
System.out.println( "Your number must be greater than 0 please try again. " );
Number = keyboard.nextInt();
}
while ( Number != 1 )
{
if ( Number % 2 == 0 )
{
Number = Number / 2;
System.out.println( Number);
}
else if ( Number % 2 != 0 )
{
Number = (Number * 3) + 1;
System.out.println( Number);
}
}
}
}
Picture of the output