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