Assignemnt #76: Collatz Sequence
Code
/// Name: Kelsey Lieberman
/// Period 5
/// Program Name: CollatzSequence
/// File Name: CollatzSequence.java
/// Date Finished: 12/16/2015
import java.util.Scanner;
public class CollatzSequence
{
public static void main(String[] args) throws Exception
{
Scanner key = new Scanner(System.in);
int i, col;
System.out.print("Give me any natural number to start with: ");
i = key.nextInt();
System.out.println("Starting number is " + i + ".");
Thread.sleep(1000);
col = 1;
while (i != 1)
{
if (i%2 == 0)
{
i = i / 2;
System.out.print(i + "\t");
}
else if (i%2 != 0)
{
i = ((3 * i) + 1);
System.out.print(i + "\t");
}
if (col == 5)
{
System.out.println();
col = 0;
}
col ++;
}
System.out.println();
}
}
Picture of the output